<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>Source code</title>
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">
</head>
<body>
<div class="sourceContainer">
<pre><span class="sourceLineNo">001</span>/*-<a name="line.1"></a>
<span class="sourceLineNo">002</span> *******************************************************************************<a name="line.2"></a>
<span class="sourceLineNo">003</span> * Copyright (c) 2011, 2016 Diamond Light Source Ltd.<a name="line.3"></a>
<span class="sourceLineNo">004</span> * All rights reserved. This program and the accompanying materials<a name="line.4"></a>
<span class="sourceLineNo">005</span> * are made available under the terms of the Eclipse Public License v1.0<a name="line.5"></a>
<span class="sourceLineNo">006</span> * which accompanies this distribution, and is available at<a name="line.6"></a>
<span class="sourceLineNo">007</span> * http://www.eclipse.org/legal/epl-v10.html<a name="line.7"></a>
<span class="sourceLineNo">008</span> *<a name="line.8"></a>
<span class="sourceLineNo">009</span> * Contributors:<a name="line.9"></a>
<span class="sourceLineNo">010</span> *    Peter Chang - initial API and implementation and/or initial documentation<a name="line.10"></a>
<span class="sourceLineNo">011</span> *******************************************************************************/<a name="line.11"></a>
<span class="sourceLineNo">012</span><a name="line.12"></a>
<span class="sourceLineNo">013</span>package org.eclipse.january.dataset;<a name="line.13"></a>
<span class="sourceLineNo">014</span><a name="line.14"></a>
<span class="sourceLineNo">015</span>import java.io.Serializable;<a name="line.15"></a>
<span class="sourceLineNo">016</span>import java.lang.reflect.Array;<a name="line.16"></a>
<span class="sourceLineNo">017</span>import java.util.ArrayList;<a name="line.17"></a>
<span class="sourceLineNo">018</span>import java.util.Arrays;<a name="line.18"></a>
<span class="sourceLineNo">019</span>import java.util.Comparator;<a name="line.19"></a>
<span class="sourceLineNo">020</span>import java.util.List;<a name="line.20"></a>
<span class="sourceLineNo">021</span><a name="line.21"></a>
<span class="sourceLineNo">022</span>import org.apache.commons.math3.util.MathArrays;<a name="line.22"></a>
<span class="sourceLineNo">023</span>import org.eclipse.january.DatasetException;<a name="line.23"></a>
<span class="sourceLineNo">024</span>import org.slf4j.Logger;<a name="line.24"></a>
<span class="sourceLineNo">025</span>import org.slf4j.LoggerFactory;<a name="line.25"></a>
<span class="sourceLineNo">026</span><a name="line.26"></a>
<span class="sourceLineNo">027</span>/**<a name="line.27"></a>
<span class="sourceLineNo">028</span> * Utilities for manipulating datasets<a name="line.28"></a>
<span class="sourceLineNo">029</span> */<a name="line.29"></a>
<span class="sourceLineNo">030</span>@SuppressWarnings("unchecked")<a name="line.30"></a>
<span class="sourceLineNo">031</span>public class DatasetUtils {<a name="line.31"></a>
<span class="sourceLineNo">032</span><a name="line.32"></a>
<span class="sourceLineNo">033</span>        /**<a name="line.33"></a>
<span class="sourceLineNo">034</span>         * Setup the logging facilities<a name="line.34"></a>
<span class="sourceLineNo">035</span>         */<a name="line.35"></a>
<span class="sourceLineNo">036</span>        transient protected static final Logger utilsLogger = LoggerFactory.getLogger(DatasetUtils.class);<a name="line.36"></a>
<span class="sourceLineNo">037</span><a name="line.37"></a>
<span class="sourceLineNo">038</span>        /**<a name="line.38"></a>
<span class="sourceLineNo">039</span>         * Append copy of dataset with another dataset along n-th axis<a name="line.39"></a>
<span class="sourceLineNo">040</span>         *<a name="line.40"></a>
<span class="sourceLineNo">041</span>         * @param a<a name="line.41"></a>
<span class="sourceLineNo">042</span>         * @param b<a name="line.42"></a>
<span class="sourceLineNo">043</span>         * @param axis<a name="line.43"></a>
<span class="sourceLineNo">044</span>         *            number of axis (negative number counts from last)<a name="line.44"></a>
<span class="sourceLineNo">045</span>         * @return appended dataset<a name="line.45"></a>
<span class="sourceLineNo">046</span>         */<a name="line.46"></a>
<span class="sourceLineNo">047</span>        public static Dataset append(IDataset a, IDataset b, int axis) {<a name="line.47"></a>
<span class="sourceLineNo">048</span>                final int[] ashape = a.getShape();<a name="line.48"></a>
<span class="sourceLineNo">049</span>                final int rank = ashape.length;<a name="line.49"></a>
<span class="sourceLineNo">050</span>                final int[] bshape = b.getShape();<a name="line.50"></a>
<span class="sourceLineNo">051</span>                if (rank != bshape.length) {<a name="line.51"></a>
<span class="sourceLineNo">052</span>                        throw new IllegalArgumentException("Incompatible number of dimensions");<a name="line.52"></a>
<span class="sourceLineNo">053</span>                }<a name="line.53"></a>
<span class="sourceLineNo">054</span>                axis = ShapeUtils.checkAxis(rank, axis);<a name="line.54"></a>
<span class="sourceLineNo">055</span><a name="line.55"></a>
<span class="sourceLineNo">056</span>                for (int i = 0; i &lt; rank; i++) {<a name="line.56"></a>
<span class="sourceLineNo">057</span>                        if (i != axis &amp;&amp; ashape[i] != bshape[i]) {<a name="line.57"></a>
<span class="sourceLineNo">058</span>                                throw new IllegalArgumentException("Incompatible dimensions");<a name="line.58"></a>
<span class="sourceLineNo">059</span>                        }<a name="line.59"></a>
<span class="sourceLineNo">060</span>                }<a name="line.60"></a>
<span class="sourceLineNo">061</span>                final int[] nshape = new int[rank];<a name="line.61"></a>
<span class="sourceLineNo">062</span>                for (int i = 0; i &lt; rank; i++) {<a name="line.62"></a>
<span class="sourceLineNo">063</span>                        nshape[i] = ashape[i];<a name="line.63"></a>
<span class="sourceLineNo">064</span>                }<a name="line.64"></a>
<span class="sourceLineNo">065</span>                nshape[axis] += bshape[axis];<a name="line.65"></a>
<span class="sourceLineNo">066</span>                final int ot = DTypeUtils.getDType(b);<a name="line.66"></a>
<span class="sourceLineNo">067</span>                final int dt = DTypeUtils.getDType(a);<a name="line.67"></a>
<span class="sourceLineNo">068</span>                @SuppressWarnings("deprecation")<a name="line.68"></a>
<span class="sourceLineNo">069</span>                Dataset ds = DatasetFactory.zeros(a.getElementsPerItem(), nshape, dt &gt; ot ? dt : ot);<a name="line.69"></a>
<span class="sourceLineNo">070</span>                IndexIterator iter = ds.getIterator(true);<a name="line.70"></a>
<span class="sourceLineNo">071</span>                int[] pos = iter.getPos();<a name="line.71"></a>
<span class="sourceLineNo">072</span>                while (iter.hasNext()) {<a name="line.72"></a>
<span class="sourceLineNo">073</span>                        int d = ashape[axis];<a name="line.73"></a>
<span class="sourceLineNo">074</span>                        if (pos[axis] &lt; d) {<a name="line.74"></a>
<span class="sourceLineNo">075</span>                                ds.setObjectAbs(iter.index, a.getObject(pos));<a name="line.75"></a>
<span class="sourceLineNo">076</span>                        } else {<a name="line.76"></a>
<span class="sourceLineNo">077</span>                                pos[axis] -= d;<a name="line.77"></a>
<span class="sourceLineNo">078</span>                                ds.setObjectAbs(iter.index, b.getObject(pos));<a name="line.78"></a>
<span class="sourceLineNo">079</span>                                pos[axis] += d;<a name="line.79"></a>
<span class="sourceLineNo">080</span>                        }<a name="line.80"></a>
<span class="sourceLineNo">081</span>                }<a name="line.81"></a>
<span class="sourceLineNo">082</span><a name="line.82"></a>
<span class="sourceLineNo">083</span>                return ds;<a name="line.83"></a>
<span class="sourceLineNo">084</span>        }<a name="line.84"></a>
<span class="sourceLineNo">085</span><a name="line.85"></a>
<span class="sourceLineNo">086</span>        /**<a name="line.86"></a>
<span class="sourceLineNo">087</span>         * Changes specific items of dataset by replacing them with other array<a name="line.87"></a>
<span class="sourceLineNo">088</span>         * @param a<a name="line.88"></a>
<span class="sourceLineNo">089</span>         * @param indices dataset interpreted as integers<a name="line.89"></a>
<span class="sourceLineNo">090</span>         * @param values<a name="line.90"></a>
<span class="sourceLineNo">091</span>         * @return changed dataset<a name="line.91"></a>
<span class="sourceLineNo">092</span>         */<a name="line.92"></a>
<span class="sourceLineNo">093</span>        public static &lt;T extends Dataset&gt; T put(final T a, final Dataset indices, Object values) {<a name="line.93"></a>
<span class="sourceLineNo">094</span>                IndexIterator it = indices.getIterator();<a name="line.94"></a>
<span class="sourceLineNo">095</span>                Dataset vd = DatasetFactory.createFromObject(values).flatten();<a name="line.95"></a>
<span class="sourceLineNo">096</span>                int vlen = vd.getSize();<a name="line.96"></a>
<span class="sourceLineNo">097</span>                int v = 0;<a name="line.97"></a>
<span class="sourceLineNo">098</span>                while (it.hasNext()) {<a name="line.98"></a>
<span class="sourceLineNo">099</span>                        if (v &gt;= vlen) v -= vlen;<a name="line.99"></a>
<span class="sourceLineNo">100</span><a name="line.100"></a>
<span class="sourceLineNo">101</span>                        a.setObjectAbs((int) indices.getElementLongAbs(it.index), vd.getObjectAbs(v++));<a name="line.101"></a>
<span class="sourceLineNo">102</span>                }<a name="line.102"></a>
<span class="sourceLineNo">103</span>                return a;<a name="line.103"></a>
<span class="sourceLineNo">104</span>        }<a name="line.104"></a>
<span class="sourceLineNo">105</span><a name="line.105"></a>
<span class="sourceLineNo">106</span>        /**<a name="line.106"></a>
<span class="sourceLineNo">107</span>         * Changes specific items of dataset by replacing them with other array<a name="line.107"></a>
<span class="sourceLineNo">108</span>         * @param a<a name="line.108"></a>
<span class="sourceLineNo">109</span>         * @param indices<a name="line.109"></a>
<span class="sourceLineNo">110</span>         * @param values<a name="line.110"></a>
<span class="sourceLineNo">111</span>         * @return changed dataset<a name="line.111"></a>
<span class="sourceLineNo">112</span>         */<a name="line.112"></a>
<span class="sourceLineNo">113</span>        public static &lt;T extends Dataset&gt; T put(final T a, final int[] indices, Object values) {<a name="line.113"></a>
<span class="sourceLineNo">114</span>                int ilen = indices.length;<a name="line.114"></a>
<span class="sourceLineNo">115</span>                Dataset vd = DatasetFactory.createFromObject(values).flatten();<a name="line.115"></a>
<span class="sourceLineNo">116</span>                int vlen = vd.getSize();<a name="line.116"></a>
<span class="sourceLineNo">117</span>                for (int i = 0, v= 0; i &lt; ilen; i++) {<a name="line.117"></a>
<span class="sourceLineNo">118</span>                        if (v &gt;= vlen) v -= vlen;<a name="line.118"></a>
<span class="sourceLineNo">119</span><a name="line.119"></a>
<span class="sourceLineNo">120</span>                        a.setObjectAbs(indices[i], vd.getObjectAbs(v++));<a name="line.120"></a>
<span class="sourceLineNo">121</span>                }<a name="line.121"></a>
<span class="sourceLineNo">122</span>                return a;<a name="line.122"></a>
<span class="sourceLineNo">123</span>        }<a name="line.123"></a>
<span class="sourceLineNo">124</span><a name="line.124"></a>
<span class="sourceLineNo">125</span>        /**<a name="line.125"></a>
<span class="sourceLineNo">126</span>         * Take items from dataset along an axis<a name="line.126"></a>
<span class="sourceLineNo">127</span>         * @param indices dataset interpreted as integers<a name="line.127"></a>
<span class="sourceLineNo">128</span>         * @param axis if null, then use flattened view<a name="line.128"></a>
<span class="sourceLineNo">129</span>         * @return a sub-array<a name="line.129"></a>
<span class="sourceLineNo">130</span>         */<a name="line.130"></a>
<span class="sourceLineNo">131</span>        public static &lt;T extends Dataset&gt; T take(final T a, final Dataset indices, Integer axis) {<a name="line.131"></a>
<span class="sourceLineNo">132</span>                IntegerDataset indexes = (IntegerDataset) indices.flatten().cast(Dataset.INT32);<a name="line.132"></a>
<span class="sourceLineNo">133</span>                return take(a, indexes.getData(), axis);<a name="line.133"></a>
<span class="sourceLineNo">134</span>        }<a name="line.134"></a>
<span class="sourceLineNo">135</span><a name="line.135"></a>
<span class="sourceLineNo">136</span>        /**<a name="line.136"></a>
<span class="sourceLineNo">137</span>         * Take items from dataset along an axis<a name="line.137"></a>
<span class="sourceLineNo">138</span>         * @param indices<a name="line.138"></a>
<span class="sourceLineNo">139</span>         * @param axis if null, then use flattened view<a name="line.139"></a>
<span class="sourceLineNo">140</span>         * @return a sub-array<a name="line.140"></a>
<span class="sourceLineNo">141</span>         */<a name="line.141"></a>
<span class="sourceLineNo">142</span>        @SuppressWarnings("deprecation")<a name="line.142"></a>
<span class="sourceLineNo">143</span>        public static &lt;T extends Dataset&gt; T take(final T a, final int[] indices, Integer axis) {<a name="line.143"></a>
<span class="sourceLineNo">144</span>                if (indices == null || indices.length == 0) {<a name="line.144"></a>
<span class="sourceLineNo">145</span>                        utilsLogger.error("No indices given");<a name="line.145"></a>
<span class="sourceLineNo">146</span>                        throw new IllegalArgumentException("No indices given");<a name="line.146"></a>
<span class="sourceLineNo">147</span>                }<a name="line.147"></a>
<span class="sourceLineNo">148</span>                int[] ashape = a.getShape();<a name="line.148"></a>
<span class="sourceLineNo">149</span>                final int rank = ashape.length;<a name="line.149"></a>
<span class="sourceLineNo">150</span>                final int at = a.getDType();<a name="line.150"></a>
<span class="sourceLineNo">151</span>                final int ilen = indices.length;<a name="line.151"></a>
<span class="sourceLineNo">152</span>                final int is = a.getElementsPerItem();<a name="line.152"></a>
<span class="sourceLineNo">153</span><a name="line.153"></a>
<span class="sourceLineNo">154</span>                Dataset result;<a name="line.154"></a>
<span class="sourceLineNo">155</span>                if (axis == null) {<a name="line.155"></a>
<span class="sourceLineNo">156</span>                        ashape = new int[1];<a name="line.156"></a>
<span class="sourceLineNo">157</span>                        ashape[0] = ilen;<a name="line.157"></a>
<span class="sourceLineNo">158</span>                        result = DatasetFactory.zeros(is, ashape, at);<a name="line.158"></a>
<span class="sourceLineNo">159</span>                        Serializable src = a.getBuffer();<a name="line.159"></a>
<span class="sourceLineNo">160</span>                        for (int i = 0; i &lt; ilen; i++) {<a name="line.160"></a>
<span class="sourceLineNo">161</span>                                ((AbstractDataset) result).setItemDirect(i, indices[i], src);<a name="line.161"></a>
<span class="sourceLineNo">162</span>                        }<a name="line.162"></a>
<span class="sourceLineNo">163</span>                } else {<a name="line.163"></a>
<span class="sourceLineNo">164</span>                        axis = a.checkAxis(axis);<a name="line.164"></a>
<span class="sourceLineNo">165</span>                        ashape[axis] = ilen;<a name="line.165"></a>
<span class="sourceLineNo">166</span>                        result = DatasetFactory.zeros(is, ashape, at);<a name="line.166"></a>
<span class="sourceLineNo">167</span><a name="line.167"></a>
<span class="sourceLineNo">168</span>                        int[] dpos = new int[rank];<a name="line.168"></a>
<span class="sourceLineNo">169</span>                        int[] spos = new int[rank];<a name="line.169"></a>
<span class="sourceLineNo">170</span>                        boolean[] axes = new boolean[rank];<a name="line.170"></a>
<span class="sourceLineNo">171</span>                        Arrays.fill(axes, true);<a name="line.171"></a>
<span class="sourceLineNo">172</span>                        axes[axis] = false;<a name="line.172"></a>
<span class="sourceLineNo">173</span>                        Serializable src = a.getBuffer();<a name="line.173"></a>
<span class="sourceLineNo">174</span>                        for (int i = 0; i &lt; ilen; i++) {<a name="line.174"></a>
<span class="sourceLineNo">175</span>                                spos[axis] = indices[i];<a name="line.175"></a>
<span class="sourceLineNo">176</span>                                dpos[axis] = i;<a name="line.176"></a>
<span class="sourceLineNo">177</span>                                SliceIterator siter = a.getSliceIteratorFromAxes(spos, axes);<a name="line.177"></a>
<span class="sourceLineNo">178</span>                                SliceIterator diter = result.getSliceIteratorFromAxes(dpos, axes);<a name="line.178"></a>
<span class="sourceLineNo">179</span><a name="line.179"></a>
<span class="sourceLineNo">180</span>                                while (siter.hasNext() &amp;&amp; diter.hasNext()) {<a name="line.180"></a>
<span class="sourceLineNo">181</span>                                        ((AbstractDataset) result).setItemDirect(diter.index, siter.index, src);<a name="line.181"></a>
<span class="sourceLineNo">182</span>                                }<a name="line.182"></a>
<span class="sourceLineNo">183</span>                        }<a name="line.183"></a>
<span class="sourceLineNo">184</span>                }<a name="line.184"></a>
<span class="sourceLineNo">185</span>                result.setDirty();<a name="line.185"></a>
<span class="sourceLineNo">186</span>                return (T) result;<a name="line.186"></a>
<span class="sourceLineNo">187</span>        }<a name="line.187"></a>
<span class="sourceLineNo">188</span><a name="line.188"></a>
<span class="sourceLineNo">189</span>        /**<a name="line.189"></a>
<span class="sourceLineNo">190</span>         * Construct a dataset that contains the original dataset repeated the number<a name="line.190"></a>
<span class="sourceLineNo">191</span>         * of times in each axis given by corresponding entries in the reps array<a name="line.191"></a>
<span class="sourceLineNo">192</span>         *<a name="line.192"></a>
<span class="sourceLineNo">193</span>         * @param a<a name="line.193"></a>
<span class="sourceLineNo">194</span>         * @param reps<a name="line.194"></a>
<span class="sourceLineNo">195</span>         * @return tiled dataset<a name="line.195"></a>
<span class="sourceLineNo">196</span>         */<a name="line.196"></a>
<span class="sourceLineNo">197</span>        public static Dataset tile(final IDataset a, int... reps) {<a name="line.197"></a>
<span class="sourceLineNo">198</span>                int[] shape = a.getShape();<a name="line.198"></a>
<span class="sourceLineNo">199</span>                int rank = shape.length;<a name="line.199"></a>
<span class="sourceLineNo">200</span>                final int rlen = reps.length;<a name="line.200"></a>
<span class="sourceLineNo">201</span><a name="line.201"></a>
<span class="sourceLineNo">202</span>                // expand shape<a name="line.202"></a>
<span class="sourceLineNo">203</span>                if (rank &lt; rlen) {<a name="line.203"></a>
<span class="sourceLineNo">204</span>                        int[] newShape = new int[rlen];<a name="line.204"></a>
<span class="sourceLineNo">205</span>                        int extraRank = rlen - rank;<a name="line.205"></a>
<span class="sourceLineNo">206</span>                        for (int i = 0; i &lt; extraRank; i++) {<a name="line.206"></a>
<span class="sourceLineNo">207</span>                                newShape[i] = 1;<a name="line.207"></a>
<span class="sourceLineNo">208</span>                        }<a name="line.208"></a>
<span class="sourceLineNo">209</span>                        for (int i = 0; i &lt; rank; i++) {<a name="line.209"></a>
<span class="sourceLineNo">210</span>                                newShape[i+extraRank] = shape[i];<a name="line.210"></a>
<span class="sourceLineNo">211</span>                        }<a name="line.211"></a>
<span class="sourceLineNo">212</span><a name="line.212"></a>
<span class="sourceLineNo">213</span>                        shape = newShape;<a name="line.213"></a>
<span class="sourceLineNo">214</span>                        rank = rlen;<a name="line.214"></a>
<span class="sourceLineNo">215</span>                } else if (rank &gt; rlen) {<a name="line.215"></a>
<span class="sourceLineNo">216</span>                        int[] newReps = new int[rank];<a name="line.216"></a>
<span class="sourceLineNo">217</span>                        int extraRank = rank - rlen;<a name="line.217"></a>
<span class="sourceLineNo">218</span>                        for (int i = 0; i &lt; extraRank; i++) {<a name="line.218"></a>
<span class="sourceLineNo">219</span>                                newReps[i] = 1;<a name="line.219"></a>
<span class="sourceLineNo">220</span>                        }<a name="line.220"></a>
<span class="sourceLineNo">221</span>                        for (int i = 0; i &lt; rlen; i++) {<a name="line.221"></a>
<span class="sourceLineNo">222</span>                                newReps[i+extraRank] = reps[i];<a name="line.222"></a>
<span class="sourceLineNo">223</span>                        }<a name="line.223"></a>
<span class="sourceLineNo">224</span>                        reps = newReps;<a name="line.224"></a>
<span class="sourceLineNo">225</span>                }<a name="line.225"></a>
<span class="sourceLineNo">226</span><a name="line.226"></a>
<span class="sourceLineNo">227</span>                // calculate new shape<a name="line.227"></a>
<span class="sourceLineNo">228</span>                int[] newShape = new int[rank];<a name="line.228"></a>
<span class="sourceLineNo">229</span>                for (int i = 0; i &lt; rank; i++) {<a name="line.229"></a>
<span class="sourceLineNo">230</span>                        newShape[i] = shape[i]*reps[i];<a name="line.230"></a>
<span class="sourceLineNo">231</span>                }<a name="line.231"></a>
<span class="sourceLineNo">232</span><a name="line.232"></a>
<span class="sourceLineNo">233</span>                @SuppressWarnings("deprecation")<a name="line.233"></a>
<span class="sourceLineNo">234</span>                Dataset tdata = DatasetFactory.zeros(a.getElementsPerItem(), newShape, DTypeUtils.getDType(a));<a name="line.234"></a>
<span class="sourceLineNo">235</span><a name="line.235"></a>
<span class="sourceLineNo">236</span>                // decide which way to put slices<a name="line.236"></a>
<span class="sourceLineNo">237</span>                boolean manyColumns;<a name="line.237"></a>
<span class="sourceLineNo">238</span>                if (rank == 1)<a name="line.238"></a>
<span class="sourceLineNo">239</span>                        manyColumns = true;<a name="line.239"></a>
<span class="sourceLineNo">240</span>                else<a name="line.240"></a>
<span class="sourceLineNo">241</span>                        manyColumns = shape[rank-1] &gt; 64;<a name="line.241"></a>
<span class="sourceLineNo">242</span><a name="line.242"></a>
<span class="sourceLineNo">243</span>                if (manyColumns) {<a name="line.243"></a>
<span class="sourceLineNo">244</span>                        // generate each start point and put a slice in<a name="line.244"></a>
<span class="sourceLineNo">245</span>                        IndexIterator iter = tdata.getSliceIterator(null, null, shape);<a name="line.245"></a>
<span class="sourceLineNo">246</span>                        SliceIterator siter = (SliceIterator) tdata.getSliceIterator(null, shape, null);<a name="line.246"></a>
<span class="sourceLineNo">247</span>                        final int[] pos = iter.getPos();<a name="line.247"></a>
<span class="sourceLineNo">248</span>                        while (iter.hasNext()) {<a name="line.248"></a>
<span class="sourceLineNo">249</span>                                siter.setStart(pos);<a name="line.249"></a>
<span class="sourceLineNo">250</span>                                tdata.setSlice(a, siter);<a name="line.250"></a>
<span class="sourceLineNo">251</span>                        }<a name="line.251"></a>
<span class="sourceLineNo">252</span><a name="line.252"></a>
<span class="sourceLineNo">253</span>                } else {<a name="line.253"></a>
<span class="sourceLineNo">254</span>                        // for each value, set slice given by repeats<a name="line.254"></a>
<span class="sourceLineNo">255</span>                        final int[] skip = new int[rank];<a name="line.255"></a>
<span class="sourceLineNo">256</span>                        for (int i = 0; i &lt; rank; i++) {<a name="line.256"></a>
<span class="sourceLineNo">257</span>                                if (reps[i] == 1) {<a name="line.257"></a>
<span class="sourceLineNo">258</span>                                        skip[i] = newShape[i];<a name="line.258"></a>
<span class="sourceLineNo">259</span>                                } else {<a name="line.259"></a>
<span class="sourceLineNo">260</span>                                        skip[i] = shape[i];<a name="line.260"></a>
<span class="sourceLineNo">261</span>                                }<a name="line.261"></a>
<span class="sourceLineNo">262</span>                        }<a name="line.262"></a>
<span class="sourceLineNo">263</span><a name="line.263"></a>
<span class="sourceLineNo">264</span>                        Dataset aa = convertToDataset(a);<a name="line.264"></a>
<span class="sourceLineNo">265</span>                        IndexIterator ita = aa.getIterator(true);<a name="line.265"></a>
<span class="sourceLineNo">266</span>                        final int[] pos = ita.getPos();<a name="line.266"></a>
<span class="sourceLineNo">267</span><a name="line.267"></a>
<span class="sourceLineNo">268</span>                        final int[] sstart = new int[rank];<a name="line.268"></a>
<span class="sourceLineNo">269</span>                        final int extra = rank - pos.length;<a name="line.269"></a>
<span class="sourceLineNo">270</span>                        for (int i = 0; i &lt; extra; i++) {<a name="line.270"></a>
<span class="sourceLineNo">271</span>                                sstart[i] = 0;<a name="line.271"></a>
<span class="sourceLineNo">272</span>                        }<a name="line.272"></a>
<span class="sourceLineNo">273</span>                        SliceIterator siter = (SliceIterator) tdata.getSliceIterator(sstart, null, skip);<a name="line.273"></a>
<span class="sourceLineNo">274</span>                        while (ita.hasNext()) {<a name="line.274"></a>
<span class="sourceLineNo">275</span>                                for (int i = 0; i &lt; pos.length; i++) {<a name="line.275"></a>
<span class="sourceLineNo">276</span>                                        sstart[i + extra] = pos[i];<a name="line.276"></a>
<span class="sourceLineNo">277</span>                                }<a name="line.277"></a>
<span class="sourceLineNo">278</span>                                siter.setStart(sstart);<a name="line.278"></a>
<span class="sourceLineNo">279</span>                                tdata.setSlice(aa.getObjectAbs(ita.index), siter);<a name="line.279"></a>
<span class="sourceLineNo">280</span>                        }<a name="line.280"></a>
<span class="sourceLineNo">281</span>                }<a name="line.281"></a>
<span class="sourceLineNo">282</span><a name="line.282"></a>
<span class="sourceLineNo">283</span>                return tdata;<a name="line.283"></a>
<span class="sourceLineNo">284</span>        }<a name="line.284"></a>
<span class="sourceLineNo">285</span><a name="line.285"></a>
<span class="sourceLineNo">286</span>        /**<a name="line.286"></a>
<span class="sourceLineNo">287</span>         * Permute copy of dataset's axes so that given order is old order:<a name="line.287"></a>
<span class="sourceLineNo">288</span>         * &lt;pre&gt;<a name="line.288"></a>
<span class="sourceLineNo">289</span>         *  axisPerm = (p(0), p(1),...) =&gt; newdata(n(0), n(1),...) = olddata(o(0), o(1), ...)<a name="line.289"></a>
<span class="sourceLineNo">290</span>         *  such that n(i) = o(p(i)) for all i<a name="line.290"></a>
<span class="sourceLineNo">291</span>         * &lt;/pre&gt;<a name="line.291"></a>
<span class="sourceLineNo">292</span>         * I.e. for a 3D dataset (1,0,2) implies the new dataset has its 1st dimension<a name="line.292"></a>
<span class="sourceLineNo">293</span>         * running along the old dataset's 2nd dimension and the new 2nd is the old 1st.<a name="line.293"></a>
<span class="sourceLineNo">294</span>         * The 3rd dimension is left unchanged.<a name="line.294"></a>
<span class="sourceLineNo">295</span>         *<a name="line.295"></a>
<span class="sourceLineNo">296</span>         * @param a<a name="line.296"></a>
<span class="sourceLineNo">297</span>         * @param axes if null or zero length then axes order reversed<a name="line.297"></a>
<span class="sourceLineNo">298</span>         * @return remapped copy of data<a name="line.298"></a>
<span class="sourceLineNo">299</span>         */<a name="line.299"></a>
<span class="sourceLineNo">300</span>        public static Dataset transpose(final IDataset a, int... axes) {<a name="line.300"></a>
<span class="sourceLineNo">301</span>                return convertToDataset(a).transpose(axes);<a name="line.301"></a>
<span class="sourceLineNo">302</span>        }<a name="line.302"></a>
<span class="sourceLineNo">303</span><a name="line.303"></a>
<span class="sourceLineNo">304</span>        /**<a name="line.304"></a>
<span class="sourceLineNo">305</span>         * Swap two axes in dataset<a name="line.305"></a>
<span class="sourceLineNo">306</span>         * @param a<a name="line.306"></a>
<span class="sourceLineNo">307</span>         * @param axis1<a name="line.307"></a>
<span class="sourceLineNo">308</span>         * @param axis2<a name="line.308"></a>
<span class="sourceLineNo">309</span>         * @return swapped dataset<a name="line.309"></a>
<span class="sourceLineNo">310</span>         */<a name="line.310"></a>
<span class="sourceLineNo">311</span>        public static Dataset swapAxes(final IDataset a, int axis1, int axis2) {<a name="line.311"></a>
<span class="sourceLineNo">312</span>                return convertToDataset(a).swapAxes(axis1, axis2);<a name="line.312"></a>
<span class="sourceLineNo">313</span>        }<a name="line.313"></a>
<span class="sourceLineNo">314</span><a name="line.314"></a>
<span class="sourceLineNo">315</span>        /**<a name="line.315"></a>
<span class="sourceLineNo">316</span>         * @param a<a name="line.316"></a>
<span class="sourceLineNo">317</span>         * @return sorted flattened copy of dataset<a name="line.317"></a>
<span class="sourceLineNo">318</span>         */<a name="line.318"></a>
<span class="sourceLineNo">319</span>        public static &lt;T extends Dataset&gt; T sort(final T a) {<a name="line.319"></a>
<span class="sourceLineNo">320</span>                return sort(a, (Integer) null);<a name="line.320"></a>
<span class="sourceLineNo">321</span>        }<a name="line.321"></a>
<span class="sourceLineNo">322</span><a name="line.322"></a>
<span class="sourceLineNo">323</span>        /**<a name="line.323"></a>
<span class="sourceLineNo">324</span>         * @param a<a name="line.324"></a>
<span class="sourceLineNo">325</span>         * @param axis to sort along, if null then dataset is first flattened<a name="line.325"></a>
<span class="sourceLineNo">326</span>         * @return dataset sorted along axis<a name="line.326"></a>
<span class="sourceLineNo">327</span>         */<a name="line.327"></a>
<span class="sourceLineNo">328</span>        public static &lt;T extends Dataset&gt; T sort(final T a, final Integer axis) {<a name="line.328"></a>
<span class="sourceLineNo">329</span>                Dataset s = a.clone();<a name="line.329"></a>
<span class="sourceLineNo">330</span>                return (T) s.sort(axis);<a name="line.330"></a>
<span class="sourceLineNo">331</span>        }<a name="line.331"></a>
<span class="sourceLineNo">332</span><a name="line.332"></a>
<span class="sourceLineNo">333</span>        /**<a name="line.333"></a>
<span class="sourceLineNo">334</span>         * Sort in place given dataset and reorder ancillary datasets too<a name="line.334"></a>
<span class="sourceLineNo">335</span>         * @param a dataset to be sorted<a name="line.335"></a>
<span class="sourceLineNo">336</span>         * @param b ancillary datasets<a name="line.336"></a>
<span class="sourceLineNo">337</span>         */<a name="line.337"></a>
<span class="sourceLineNo">338</span>        public static void sort(Dataset a, Dataset... b) {<a name="line.338"></a>
<span class="sourceLineNo">339</span>                if (!DTypeUtils.isDTypeNumerical(a.getDType())) {<a name="line.339"></a>
<span class="sourceLineNo">340</span>                        throw new UnsupportedOperationException("Sorting non-numerical datasets not supported yet");<a name="line.340"></a>
<span class="sourceLineNo">341</span>                }<a name="line.341"></a>
<span class="sourceLineNo">342</span><a name="line.342"></a>
<span class="sourceLineNo">343</span>                // gather all datasets as double dataset copies<a name="line.343"></a>
<span class="sourceLineNo">344</span>                DoubleDataset s = copy(DoubleDataset.class, a);<a name="line.344"></a>
<span class="sourceLineNo">345</span>                int l = b == null ? 0 : b.length;<a name="line.345"></a>
<span class="sourceLineNo">346</span>                DoubleDataset[] t = new DoubleDataset[l];<a name="line.346"></a>
<span class="sourceLineNo">347</span>                int n = 0;<a name="line.347"></a>
<span class="sourceLineNo">348</span>                for (int i = 0; i &lt; l; i++) {<a name="line.348"></a>
<span class="sourceLineNo">349</span>                        if (b[i] != null) {<a name="line.349"></a>
<span class="sourceLineNo">350</span>                                if (!DTypeUtils.isDTypeNumerical(b[i].getDType())) {<a name="line.350"></a>
<span class="sourceLineNo">351</span>                                        throw new UnsupportedOperationException("Sorting non-numerical datasets not supported yet");<a name="line.351"></a>
<span class="sourceLineNo">352</span>                                }<a name="line.352"></a>
<span class="sourceLineNo">353</span>                                t[i] = copy(DoubleDataset.class, b[i]);<a name="line.353"></a>
<span class="sourceLineNo">354</span>                                n++;<a name="line.354"></a>
<span class="sourceLineNo">355</span>                        }<a name="line.355"></a>
<span class="sourceLineNo">356</span>                }<a name="line.356"></a>
<span class="sourceLineNo">357</span><a name="line.357"></a>
<span class="sourceLineNo">358</span>                double[][] y = new double[n][];<a name="line.358"></a>
<span class="sourceLineNo">359</span>                for (int i = 0, j = 0; i &lt; l; i++) {<a name="line.359"></a>
<span class="sourceLineNo">360</span>                        if (t[i] != null) {<a name="line.360"></a>
<span class="sourceLineNo">361</span>                                y[j++] = t[i].getData();<a name="line.361"></a>
<span class="sourceLineNo">362</span>                        }<a name="line.362"></a>
<span class="sourceLineNo">363</span>                }<a name="line.363"></a>
<span class="sourceLineNo">364</span><a name="line.364"></a>
<span class="sourceLineNo">365</span>                MathArrays.sortInPlace(s.getData(), y);<a name="line.365"></a>
<span class="sourceLineNo">366</span><a name="line.366"></a>
<span class="sourceLineNo">367</span>                a.setSlice(s);<a name="line.367"></a>
<span class="sourceLineNo">368</span>                for (int i = 0; i &lt; l; i++) {<a name="line.368"></a>
<span class="sourceLineNo">369</span>                        if (b[i] != null) {<a name="line.369"></a>
<span class="sourceLineNo">370</span>                                b[i].setSlice(t[i]);<a name="line.370"></a>
<span class="sourceLineNo">371</span>                        }<a name="line.371"></a>
<span class="sourceLineNo">372</span>                }<a name="line.372"></a>
<span class="sourceLineNo">373</span>        }<a name="line.373"></a>
<span class="sourceLineNo">374</span><a name="line.374"></a>
<span class="sourceLineNo">375</span>        /**<a name="line.375"></a>
<span class="sourceLineNo">376</span>         * Indirectly sort along given axis<a name="line.376"></a>
<span class="sourceLineNo">377</span>         * @param a dataset whose indexes will be sorted<a name="line.377"></a>
<span class="sourceLineNo">378</span>         * @param axis to sort along, if null then dataset is first flattened<a name="line.378"></a>
<span class="sourceLineNo">379</span>         * @return indexes<a name="line.379"></a>
<span class="sourceLineNo">380</span>         * @since 2.1<a name="line.380"></a>
<span class="sourceLineNo">381</span>         */<a name="line.381"></a>
<span class="sourceLineNo">382</span>        public static IntegerDataset indexSort(Dataset a, Integer axis) {<a name="line.382"></a>
<span class="sourceLineNo">383</span>                if (axis == null) {<a name="line.383"></a>
<span class="sourceLineNo">384</span>                        int size = a.getSize();<a name="line.384"></a>
<span class="sourceLineNo">385</span>                        Integer[] index = new Integer[size];<a name="line.385"></a>
<span class="sourceLineNo">386</span>                        for (int i = 0; i &lt; size; i++) {<a name="line.386"></a>
<span class="sourceLineNo">387</span>                                index[i] = i;<a name="line.387"></a>
<span class="sourceLineNo">388</span>                        }<a name="line.388"></a>
<span class="sourceLineNo">389</span>                        final Dataset f = a.flatten(); // is this correct for views??? Check with NumPy<a name="line.389"></a>
<span class="sourceLineNo">390</span>                        Comparator&lt;Integer&gt; cmp = new Comparator&lt;Integer&gt;() {<a name="line.390"></a>
<span class="sourceLineNo">391</span><a name="line.391"></a>
<span class="sourceLineNo">392</span>                                @Override<a name="line.392"></a>
<span class="sourceLineNo">393</span>                                public int compare(Integer o1, Integer o2) {<a name="line.393"></a>
<span class="sourceLineNo">394</span><a name="line.394"></a>
<span class="sourceLineNo">395</span>                                        return Double.compare(f.getElementDoubleAbs(o1), f.getElementDoubleAbs(o2));<a name="line.395"></a>
<span class="sourceLineNo">396</span>                                }<a name="line.396"></a>
<span class="sourceLineNo">397</span>                        };<a name="line.397"></a>
<span class="sourceLineNo">398</span>                        Arrays.sort(index, cmp);<a name="line.398"></a>
<span class="sourceLineNo">399</span>                        return DatasetFactory.createFromObject(IntegerDataset.class, index);<a name="line.399"></a>
<span class="sourceLineNo">400</span>                }<a name="line.400"></a>
<span class="sourceLineNo">401</span><a name="line.401"></a>
<span class="sourceLineNo">402</span>                axis = a.checkAxis(axis);<a name="line.402"></a>
<span class="sourceLineNo">403</span>                final int[] shape = a.getShapeRef();<a name="line.403"></a>
<span class="sourceLineNo">404</span>                IntegerDataset id = DatasetFactory.zeros(IntegerDataset.class, shape);<a name="line.404"></a>
<span class="sourceLineNo">405</span>                int size = shape[axis];<a name="line.405"></a>
<span class="sourceLineNo">406</span>                Integer[] index = new Integer[size];<a name="line.406"></a>
<span class="sourceLineNo">407</span><a name="line.407"></a>
<span class="sourceLineNo">408</span>                int[] dShape = new int[shape.length];<a name="line.408"></a>
<span class="sourceLineNo">409</span>                Arrays.fill(dShape, 1);<a name="line.409"></a>
<span class="sourceLineNo">410</span>                dShape[axis] = size;<a name="line.410"></a>
<span class="sourceLineNo">411</span>                final DoubleDataset dd = DatasetFactory.zeros(DoubleDataset.class, dShape);<a name="line.411"></a>
<span class="sourceLineNo">412</span>                final Comparator&lt;Integer&gt; cmp = new Comparator&lt;Integer&gt;() {<a name="line.412"></a>
<span class="sourceLineNo">413</span>                        @Override<a name="line.413"></a>
<span class="sourceLineNo">414</span>                        public int compare(Integer o1, Integer o2) {<a name="line.414"></a>
<span class="sourceLineNo">415</span><a name="line.415"></a>
<span class="sourceLineNo">416</span>                                return Double.compare(dd.getElementDoubleAbs(o1), dd.getElementDoubleAbs(o2));<a name="line.416"></a>
<span class="sourceLineNo">417</span>                        }<a name="line.417"></a>
<span class="sourceLineNo">418</span>                };<a name="line.418"></a>
<span class="sourceLineNo">419</span><a name="line.419"></a>
<span class="sourceLineNo">420</span>                SliceND ds = new SliceND(dShape);<a name="line.420"></a>
<span class="sourceLineNo">421</span>                SliceNDIterator it = new SliceNDIterator(new SliceND(shape), axis);<a name="line.421"></a>
<span class="sourceLineNo">422</span>                int[] pos = it.getPos();<a name="line.422"></a>
<span class="sourceLineNo">423</span>                int[] ipos = pos.clone();<a name="line.423"></a>
<span class="sourceLineNo">424</span>                while (it.hasNext()) {<a name="line.424"></a>
<span class="sourceLineNo">425</span>                        dd.setSlice(a.getSliceView(it.getCurrentSlice()), ds);<a name="line.425"></a>
<span class="sourceLineNo">426</span>                        for (int i = 0; i &lt; size; i++) {<a name="line.426"></a>
<span class="sourceLineNo">427</span>                                index[i] = i;<a name="line.427"></a>
<span class="sourceLineNo">428</span>                        }<a name="line.428"></a>
<span class="sourceLineNo">429</span>                        Arrays.sort(index, cmp);<a name="line.429"></a>
<span class="sourceLineNo">430</span><a name="line.430"></a>
<span class="sourceLineNo">431</span>                        System.arraycopy(pos, 0, ipos, 0, pos.length);<a name="line.431"></a>
<span class="sourceLineNo">432</span>                        for (int i = 0; i &lt; size; i++) {<a name="line.432"></a>
<span class="sourceLineNo">433</span>                                ipos[axis] = i;<a name="line.433"></a>
<span class="sourceLineNo">434</span>                                id.set(index[i], ipos);<a name="line.434"></a>
<span class="sourceLineNo">435</span>                        }<a name="line.435"></a>
<span class="sourceLineNo">436</span>                }<a name="line.436"></a>
<span class="sourceLineNo">437</span><a name="line.437"></a>
<span class="sourceLineNo">438</span>                return id;<a name="line.438"></a>
<span class="sourceLineNo">439</span>        }<a name="line.439"></a>
<span class="sourceLineNo">440</span><a name="line.440"></a>
<span class="sourceLineNo">441</span>        /**<a name="line.441"></a>
<span class="sourceLineNo">442</span>         * Concatenate the set of datasets along given axis<a name="line.442"></a>
<span class="sourceLineNo">443</span>         * @param as<a name="line.443"></a>
<span class="sourceLineNo">444</span>         * @param axis<a name="line.444"></a>
<span class="sourceLineNo">445</span>         * @return concatenated dataset<a name="line.445"></a>
<span class="sourceLineNo">446</span>         */<a name="line.446"></a>
<span class="sourceLineNo">447</span>        public static Dataset concatenate(final IDataset[] as, int axis) {<a name="line.447"></a>
<span class="sourceLineNo">448</span>                if (as == null || as.length == 0) {<a name="line.448"></a>
<span class="sourceLineNo">449</span>                        utilsLogger.error("No datasets given");<a name="line.449"></a>
<span class="sourceLineNo">450</span>                        throw new IllegalArgumentException("No datasets given");<a name="line.450"></a>
<span class="sourceLineNo">451</span>                }<a name="line.451"></a>
<span class="sourceLineNo">452</span>                IDataset a = as[0];<a name="line.452"></a>
<span class="sourceLineNo">453</span>                if (as.length == 1) {<a name="line.453"></a>
<span class="sourceLineNo">454</span>                        return convertToDataset(a.clone());<a name="line.454"></a>
<span class="sourceLineNo">455</span>                }<a name="line.455"></a>
<span class="sourceLineNo">456</span><a name="line.456"></a>
<span class="sourceLineNo">457</span>                int[] ashape = a.getShape();<a name="line.457"></a>
<span class="sourceLineNo">458</span>                axis = ShapeUtils.checkAxis(ashape.length, axis);<a name="line.458"></a>
<span class="sourceLineNo">459</span>                int at = DTypeUtils.getDType(a);<a name="line.459"></a>
<span class="sourceLineNo">460</span>                int anum = as.length;<a name="line.460"></a>
<span class="sourceLineNo">461</span>                int isize = a.getElementsPerItem();<a name="line.461"></a>
<span class="sourceLineNo">462</span><a name="line.462"></a>
<span class="sourceLineNo">463</span>                int i = 1;<a name="line.463"></a>
<span class="sourceLineNo">464</span>                for (; i &lt; anum; i++) {<a name="line.464"></a>
<span class="sourceLineNo">465</span>                        if (at != DTypeUtils.getDType(as[i])) {<a name="line.465"></a>
<span class="sourceLineNo">466</span>                                utilsLogger.error("Datasets are not of same type");<a name="line.466"></a>
<span class="sourceLineNo">467</span>                                break;<a name="line.467"></a>
<span class="sourceLineNo">468</span>                        }<a name="line.468"></a>
<span class="sourceLineNo">469</span>                        if (!ShapeUtils.areShapesCompatible(ashape, as[i].getShape(), axis)) {<a name="line.469"></a>
<span class="sourceLineNo">470</span>                                utilsLogger.error("Datasets' shapes are not equal");<a name="line.470"></a>
<span class="sourceLineNo">471</span>                                break;<a name="line.471"></a>
<span class="sourceLineNo">472</span>                        }<a name="line.472"></a>
<span class="sourceLineNo">473</span>                        final int is = as[i].getElementsPerItem();<a name="line.473"></a>
<span class="sourceLineNo">474</span>                        if (isize &lt; is)<a name="line.474"></a>
<span class="sourceLineNo">475</span>                                isize = is;<a name="line.475"></a>
<span class="sourceLineNo">476</span>                }<a name="line.476"></a>
<span class="sourceLineNo">477</span>                if (i &lt; anum) {<a name="line.477"></a>
<span class="sourceLineNo">478</span>                        utilsLogger.error("Dataset are not compatible");<a name="line.478"></a>
<span class="sourceLineNo">479</span>                        throw new IllegalArgumentException("Datasets are not compatible");<a name="line.479"></a>
<span class="sourceLineNo">480</span>                }<a name="line.480"></a>
<span class="sourceLineNo">481</span><a name="line.481"></a>
<span class="sourceLineNo">482</span>                for (i = 1; i &lt; anum; i++) {<a name="line.482"></a>
<span class="sourceLineNo">483</span>                        ashape[axis] += as[i].getShape()[axis];<a name="line.483"></a>
<span class="sourceLineNo">484</span>                }<a name="line.484"></a>
<span class="sourceLineNo">485</span><a name="line.485"></a>
<span class="sourceLineNo">486</span>                @SuppressWarnings("deprecation")<a name="line.486"></a>
<span class="sourceLineNo">487</span>                Dataset result = DatasetFactory.zeros(isize, ashape, at);<a name="line.487"></a>
<span class="sourceLineNo">488</span><a name="line.488"></a>
<span class="sourceLineNo">489</span>                int[] start = new int[ashape.length];<a name="line.489"></a>
<span class="sourceLineNo">490</span>                int[] stop = ashape;<a name="line.490"></a>
<span class="sourceLineNo">491</span>                stop[axis] = 0;<a name="line.491"></a>
<span class="sourceLineNo">492</span>                for (i = 0; i &lt; anum; i++) {<a name="line.492"></a>
<span class="sourceLineNo">493</span>                        IDataset b = as[i];<a name="line.493"></a>
<span class="sourceLineNo">494</span>                        int[] bshape = b.getShape();<a name="line.494"></a>
<span class="sourceLineNo">495</span>                        stop[axis] += bshape[axis];<a name="line.495"></a>
<span class="sourceLineNo">496</span>                        result.setSlice(b, start, stop, null);<a name="line.496"></a>
<span class="sourceLineNo">497</span>                        start[axis] += bshape[axis];<a name="line.497"></a>
<span class="sourceLineNo">498</span>                }<a name="line.498"></a>
<span class="sourceLineNo">499</span><a name="line.499"></a>
<span class="sourceLineNo">500</span>                return result;<a name="line.500"></a>
<span class="sourceLineNo">501</span>        }<a name="line.501"></a>
<span class="sourceLineNo">502</span><a name="line.502"></a>
<span class="sourceLineNo">503</span>        /**<a name="line.503"></a>
<span class="sourceLineNo">504</span>         * Split a dataset into equal sections along given axis<a name="line.504"></a>
<span class="sourceLineNo">505</span>         * @param a<a name="line.505"></a>
<span class="sourceLineNo">506</span>         * @param sections<a name="line.506"></a>
<span class="sourceLineNo">507</span>         * @param axis<a name="line.507"></a>
<span class="sourceLineNo">508</span>         * @param checkEqual makes sure the division is into equal parts<a name="line.508"></a>
<span class="sourceLineNo">509</span>         * @return list of split datasets<a name="line.509"></a>
<span class="sourceLineNo">510</span>         */<a name="line.510"></a>
<span class="sourceLineNo">511</span>        public static List&lt;Dataset&gt; split(final Dataset a, int sections, int axis, final boolean checkEqual) {<a name="line.511"></a>
<span class="sourceLineNo">512</span>                int[] ashape = a.getShapeRef();<a name="line.512"></a>
<span class="sourceLineNo">513</span>                axis = a.checkAxis(axis);<a name="line.513"></a>
<span class="sourceLineNo">514</span>                int imax = ashape[axis];<a name="line.514"></a>
<span class="sourceLineNo">515</span>                if (checkEqual &amp;&amp; (imax%sections) != 0) {<a name="line.515"></a>
<span class="sourceLineNo">516</span>                        utilsLogger.error("Number of sections does not divide axis into equal parts");<a name="line.516"></a>
<span class="sourceLineNo">517</span>                        throw new IllegalArgumentException("Number of sections does not divide axis into equal parts");<a name="line.517"></a>
<span class="sourceLineNo">518</span>                }<a name="line.518"></a>
<span class="sourceLineNo">519</span>                int n = (imax + sections - 1) / sections;<a name="line.519"></a>
<span class="sourceLineNo">520</span>                int[] indices = new int[sections-1];<a name="line.520"></a>
<span class="sourceLineNo">521</span>                for (int i = 1; i &lt; sections; i++)<a name="line.521"></a>
<span class="sourceLineNo">522</span>                        indices[i-1] = n*i;<a name="line.522"></a>
<span class="sourceLineNo">523</span>                return split(a, indices, axis);<a name="line.523"></a>
<span class="sourceLineNo">524</span>        }<a name="line.524"></a>
<span class="sourceLineNo">525</span><a name="line.525"></a>
<span class="sourceLineNo">526</span>        /**<a name="line.526"></a>
<span class="sourceLineNo">527</span>         * Split a dataset into parts along given axis<a name="line.527"></a>
<span class="sourceLineNo">528</span>         * @param a<a name="line.528"></a>
<span class="sourceLineNo">529</span>         * @param indices<a name="line.529"></a>
<span class="sourceLineNo">530</span>         * @param axis<a name="line.530"></a>
<span class="sourceLineNo">531</span>         * @return list of split datasets<a name="line.531"></a>
<span class="sourceLineNo">532</span>         */<a name="line.532"></a>
<span class="sourceLineNo">533</span>        public static List&lt;Dataset&gt; split(final Dataset a, int[] indices, int axis) {<a name="line.533"></a>
<span class="sourceLineNo">534</span>                final int[] ashape = a.getShapeRef();<a name="line.534"></a>
<span class="sourceLineNo">535</span>                axis = a.checkAxis(axis);<a name="line.535"></a>
<span class="sourceLineNo">536</span>                final int rank = ashape.length;<a name="line.536"></a>
<span class="sourceLineNo">537</span>                final int imax = ashape[axis];<a name="line.537"></a>
<span class="sourceLineNo">538</span><a name="line.538"></a>
<span class="sourceLineNo">539</span>                final List&lt;Dataset&gt; result = new ArrayList&lt;Dataset&gt;();<a name="line.539"></a>
<span class="sourceLineNo">540</span><a name="line.540"></a>
<span class="sourceLineNo">541</span>                final int[] nshape = ashape.clone();<a name="line.541"></a>
<span class="sourceLineNo">542</span>                final int is = a.getElementsPerItem();<a name="line.542"></a>
<span class="sourceLineNo">543</span><a name="line.543"></a>
<span class="sourceLineNo">544</span>                int oind = 0;<a name="line.544"></a>
<span class="sourceLineNo">545</span>                final int[] start = new int[rank];<a name="line.545"></a>
<span class="sourceLineNo">546</span>                final int[] stop = new int[rank];<a name="line.546"></a>
<span class="sourceLineNo">547</span>                final int[] step = new int[rank];<a name="line.547"></a>
<span class="sourceLineNo">548</span>                for (int i = 0; i &lt; rank; i++) {<a name="line.548"></a>
<span class="sourceLineNo">549</span>                        start[i] = 0;<a name="line.549"></a>
<span class="sourceLineNo">550</span>                        stop[i] = ashape[i];<a name="line.550"></a>
<span class="sourceLineNo">551</span>                        step[i] = 1;<a name="line.551"></a>
<span class="sourceLineNo">552</span>                }<a name="line.552"></a>
<span class="sourceLineNo">553</span>                for (int ind : indices) {<a name="line.553"></a>
<span class="sourceLineNo">554</span>                        if (ind &gt; imax) {<a name="line.554"></a>
<span class="sourceLineNo">555</span>                                result.add(DatasetFactory.zeros(is, a.getClass(), 0));<a name="line.555"></a>
<span class="sourceLineNo">556</span>                        } else {<a name="line.556"></a>
<span class="sourceLineNo">557</span>                                nshape[axis] = ind - oind;<a name="line.557"></a>
<span class="sourceLineNo">558</span>                                start[axis] = oind;<a name="line.558"></a>
<span class="sourceLineNo">559</span>                                stop[axis] = ind;<a name="line.559"></a>
<span class="sourceLineNo">560</span>                                Dataset n = DatasetFactory.zeros(is, a.getClass(), nshape);<a name="line.560"></a>
<span class="sourceLineNo">561</span>                                IndexIterator iter = a.getSliceIterator(start, stop, step);<a name="line.561"></a>
<span class="sourceLineNo">562</span><a name="line.562"></a>
<span class="sourceLineNo">563</span>                                a.fillDataset(n, iter);<a name="line.563"></a>
<span class="sourceLineNo">564</span>                                result.add(n);<a name="line.564"></a>
<span class="sourceLineNo">565</span>                                oind = ind;<a name="line.565"></a>
<span class="sourceLineNo">566</span>                        }<a name="line.566"></a>
<span class="sourceLineNo">567</span>                }<a name="line.567"></a>
<span class="sourceLineNo">568</span><a name="line.568"></a>
<span class="sourceLineNo">569</span>                if (imax &gt; oind) {<a name="line.569"></a>
<span class="sourceLineNo">570</span>                        nshape[axis] = imax - oind;<a name="line.570"></a>
<span class="sourceLineNo">571</span>                        start[axis] = oind;<a name="line.571"></a>
<span class="sourceLineNo">572</span>                        stop[axis] = imax;<a name="line.572"></a>
<span class="sourceLineNo">573</span>                        Dataset n = DatasetFactory.zeros(is, a.getClass(), nshape);<a name="line.573"></a>
<span class="sourceLineNo">574</span>                        IndexIterator iter = a.getSliceIterator(start, stop, step);<a name="line.574"></a>
<span class="sourceLineNo">575</span><a name="line.575"></a>
<span class="sourceLineNo">576</span>                        a.fillDataset(n, iter);<a name="line.576"></a>
<span class="sourceLineNo">577</span>                        result.add(n);<a name="line.577"></a>
<span class="sourceLineNo">578</span>                }<a name="line.578"></a>
<span class="sourceLineNo">579</span><a name="line.579"></a>
<span class="sourceLineNo">580</span>                return result;<a name="line.580"></a>
<span class="sourceLineNo">581</span>        }<a name="line.581"></a>
<span class="sourceLineNo">582</span><a name="line.582"></a>
<span class="sourceLineNo">583</span>        /**<a name="line.583"></a>
<span class="sourceLineNo">584</span>         * Constructs a dataset which has its elements along an axis replicated from<a name="line.584"></a>
<span class="sourceLineNo">585</span>         * the original dataset by the number of times given in the repeats array.<a name="line.585"></a>
<span class="sourceLineNo">586</span>         *<a name="line.586"></a>
<span class="sourceLineNo">587</span>         * By default, axis=-1 implies using a flattened version of the input dataset<a name="line.587"></a>
<span class="sourceLineNo">588</span>         *<a name="line.588"></a>
<span class="sourceLineNo">589</span>         * @param a<a name="line.589"></a>
<span class="sourceLineNo">590</span>         * @param repeats<a name="line.590"></a>
<span class="sourceLineNo">591</span>         * @param axis<a name="line.591"></a>
<span class="sourceLineNo">592</span>         * @return dataset<a name="line.592"></a>
<span class="sourceLineNo">593</span>         */<a name="line.593"></a>
<span class="sourceLineNo">594</span>        public static &lt;T extends Dataset&gt; T repeat(T a, int[] repeats, int axis) {<a name="line.594"></a>
<span class="sourceLineNo">595</span>                Serializable buf = a.getBuffer();<a name="line.595"></a>
<span class="sourceLineNo">596</span>                int[] shape = a.getShape();<a name="line.596"></a>
<span class="sourceLineNo">597</span>                int rank = shape.length;<a name="line.597"></a>
<span class="sourceLineNo">598</span>                final int is = a.getElementsPerItem();<a name="line.598"></a>
<span class="sourceLineNo">599</span><a name="line.599"></a>
<span class="sourceLineNo">600</span>                if (axis &gt;= rank) {<a name="line.600"></a>
<span class="sourceLineNo">601</span>                        utilsLogger.warn("Axis value is out of bounds");<a name="line.601"></a>
<span class="sourceLineNo">602</span>                        throw new IllegalArgumentException("Axis value is out of bounds");<a name="line.602"></a>
<span class="sourceLineNo">603</span>                }<a name="line.603"></a>
<span class="sourceLineNo">604</span><a name="line.604"></a>
<span class="sourceLineNo">605</span>                int alen;<a name="line.605"></a>
<span class="sourceLineNo">606</span>                if (axis &lt; 0) {<a name="line.606"></a>
<span class="sourceLineNo">607</span>                        alen = a.getSize();<a name="line.607"></a>
<span class="sourceLineNo">608</span>                        axis = 0;<a name="line.608"></a>
<span class="sourceLineNo">609</span>                        rank = 1;<a name="line.609"></a>
<span class="sourceLineNo">610</span>                        shape[0] = alen;<a name="line.610"></a>
<span class="sourceLineNo">611</span>                } else {<a name="line.611"></a>
<span class="sourceLineNo">612</span>                        alen = shape[axis];<a name="line.612"></a>
<span class="sourceLineNo">613</span>                }<a name="line.613"></a>
<span class="sourceLineNo">614</span>                int rlen = repeats.length;<a name="line.614"></a>
<span class="sourceLineNo">615</span>                if (rlen != 1 &amp;&amp; rlen != alen) {<a name="line.615"></a>
<span class="sourceLineNo">616</span>                        utilsLogger.warn("Repeats array should have length of 1 or match chosen axis");<a name="line.616"></a>
<span class="sourceLineNo">617</span>                        throw new IllegalArgumentException("Repeats array should have length of 1 or match chosen axis");<a name="line.617"></a>
<span class="sourceLineNo">618</span>                }<a name="line.618"></a>
<span class="sourceLineNo">619</span><a name="line.619"></a>
<span class="sourceLineNo">620</span>                for (int i = 0; i &lt; rlen; i++) {<a name="line.620"></a>
<span class="sourceLineNo">621</span>                        if (repeats[i] &lt; 0) {<a name="line.621"></a>
<span class="sourceLineNo">622</span>                                utilsLogger.warn("Negative repeat value is not allowed");<a name="line.622"></a>
<span class="sourceLineNo">623</span>                                throw new IllegalArgumentException("Negative repeat value is not allowed");<a name="line.623"></a>
<span class="sourceLineNo">624</span>                        }<a name="line.624"></a>
<span class="sourceLineNo">625</span>                }<a name="line.625"></a>
<span class="sourceLineNo">626</span><a name="line.626"></a>
<span class="sourceLineNo">627</span>                int[] newShape = new int[rank];<a name="line.627"></a>
<span class="sourceLineNo">628</span>                for (int i = 0; i &lt; rank; i ++)<a name="line.628"></a>
<span class="sourceLineNo">629</span>                        newShape[i] = shape[i];<a name="line.629"></a>
<span class="sourceLineNo">630</span><a name="line.630"></a>
<span class="sourceLineNo">631</span>                // do single repeat separately<a name="line.631"></a>
<span class="sourceLineNo">632</span>                if (repeats.length == 1) {<a name="line.632"></a>
<span class="sourceLineNo">633</span>                        newShape[axis] *= repeats[0];<a name="line.633"></a>
<span class="sourceLineNo">634</span>                } else {<a name="line.634"></a>
<span class="sourceLineNo">635</span>                        int nlen = 0;<a name="line.635"></a>
<span class="sourceLineNo">636</span>                        for (int i = 0; i &lt; alen; i++) {<a name="line.636"></a>
<span class="sourceLineNo">637</span>                                nlen += repeats[i];<a name="line.637"></a>
<span class="sourceLineNo">638</span>                        }<a name="line.638"></a>
<span class="sourceLineNo">639</span>                        newShape[axis] = nlen;<a name="line.639"></a>
<span class="sourceLineNo">640</span>                }<a name="line.640"></a>
<span class="sourceLineNo">641</span><a name="line.641"></a>
<span class="sourceLineNo">642</span>                @SuppressWarnings("deprecation")<a name="line.642"></a>
<span class="sourceLineNo">643</span>                Dataset rdata = DatasetFactory.zeros(is, newShape, a.getDType());<a name="line.643"></a>
<span class="sourceLineNo">644</span>                Serializable nbuf = rdata.getBuffer();<a name="line.644"></a>
<span class="sourceLineNo">645</span><a name="line.645"></a>
<span class="sourceLineNo">646</span>                int csize = is; // chunk size<a name="line.646"></a>
<span class="sourceLineNo">647</span>                for (int i = axis+1; i &lt; rank; i++) {<a name="line.647"></a>
<span class="sourceLineNo">648</span>                        csize *= newShape[i];<a name="line.648"></a>
<span class="sourceLineNo">649</span>                }<a name="line.649"></a>
<span class="sourceLineNo">650</span>                int nout = 1;<a name="line.650"></a>
<span class="sourceLineNo">651</span>                for (int i = 0; i &lt; axis; i++) {<a name="line.651"></a>
<span class="sourceLineNo">652</span>                        nout *= newShape[i];<a name="line.652"></a>
<span class="sourceLineNo">653</span>                }<a name="line.653"></a>
<span class="sourceLineNo">654</span><a name="line.654"></a>
<span class="sourceLineNo">655</span>                int oi = 0;<a name="line.655"></a>
<span class="sourceLineNo">656</span>                int ni = 0;<a name="line.656"></a>
<span class="sourceLineNo">657</span>                if (rlen == 1) { // do single repeat separately<a name="line.657"></a>
<span class="sourceLineNo">658</span>                        for (int i = 0; i &lt; nout; i++) {<a name="line.658"></a>
<span class="sourceLineNo">659</span>                                for (int j = 0; j &lt; shape[axis]; j++) {<a name="line.659"></a>
<span class="sourceLineNo">660</span>                                        for (int k = 0; k &lt; repeats[0]; k++) {<a name="line.660"></a>
<span class="sourceLineNo">661</span>                                                System.arraycopy(buf, oi, nbuf, ni, csize);<a name="line.661"></a>
<span class="sourceLineNo">662</span>                                                ni += csize;<a name="line.662"></a>
<span class="sourceLineNo">663</span>                                        }<a name="line.663"></a>
<span class="sourceLineNo">664</span>                                        oi += csize;<a name="line.664"></a>
<span class="sourceLineNo">665</span>                                }<a name="line.665"></a>
<span class="sourceLineNo">666</span>                        }<a name="line.666"></a>
<span class="sourceLineNo">667</span>                } else {<a name="line.667"></a>
<span class="sourceLineNo">668</span>                        for (int i = 0; i &lt; nout; i++) {<a name="line.668"></a>
<span class="sourceLineNo">669</span>                                for (int j = 0; j &lt; shape[axis]; j++) {<a name="line.669"></a>
<span class="sourceLineNo">670</span>                                        for (int k = 0; k &lt; repeats[j]; k++) {<a name="line.670"></a>
<span class="sourceLineNo">671</span>                                                System.arraycopy(buf, oi, nbuf, ni, csize);<a name="line.671"></a>
<span class="sourceLineNo">672</span>                                                ni += csize;<a name="line.672"></a>
<span class="sourceLineNo">673</span>                                        }<a name="line.673"></a>
<span class="sourceLineNo">674</span>                                        oi += csize;<a name="line.674"></a>
<span class="sourceLineNo">675</span>                                }<a name="line.675"></a>
<span class="sourceLineNo">676</span>                        }<a name="line.676"></a>
<span class="sourceLineNo">677</span>                }<a name="line.677"></a>
<span class="sourceLineNo">678</span><a name="line.678"></a>
<span class="sourceLineNo">679</span>                return (T) rdata;<a name="line.679"></a>
<span class="sourceLineNo">680</span>        }<a name="line.680"></a>
<span class="sourceLineNo">681</span><a name="line.681"></a>
<span class="sourceLineNo">682</span>        /**<a name="line.682"></a>
<span class="sourceLineNo">683</span>         * Resize a dataset<a name="line.683"></a>
<span class="sourceLineNo">684</span>         * @param a<a name="line.684"></a>
<span class="sourceLineNo">685</span>         * @param shape<a name="line.685"></a>
<span class="sourceLineNo">686</span>         * @return new dataset with new shape and items that are truncated or repeated, as necessary<a name="line.686"></a>
<span class="sourceLineNo">687</span>         */<a name="line.687"></a>
<span class="sourceLineNo">688</span>        public static &lt;T extends Dataset&gt; T resize(final T a, final int... shape) {<a name="line.688"></a>
<span class="sourceLineNo">689</span>                int size = a.getSize();<a name="line.689"></a>
<span class="sourceLineNo">690</span>                @SuppressWarnings("deprecation")<a name="line.690"></a>
<span class="sourceLineNo">691</span>                Dataset rdata = DatasetFactory.zeros(a.getElementsPerItem(), shape, a.getDType());<a name="line.691"></a>
<span class="sourceLineNo">692</span>                IndexIterator it = rdata.getIterator();<a name="line.692"></a>
<span class="sourceLineNo">693</span>                while (it.hasNext()) {<a name="line.693"></a>
<span class="sourceLineNo">694</span>                        rdata.setObjectAbs(it.index, a.getObjectAbs(it.index % size));<a name="line.694"></a>
<span class="sourceLineNo">695</span>                }<a name="line.695"></a>
<span class="sourceLineNo">696</span><a name="line.696"></a>
<span class="sourceLineNo">697</span>                return (T) rdata;<a name="line.697"></a>
<span class="sourceLineNo">698</span>        }<a name="line.698"></a>
<span class="sourceLineNo">699</span><a name="line.699"></a>
<span class="sourceLineNo">700</span>        /**<a name="line.700"></a>
<span class="sourceLineNo">701</span>         * Copy and cast a dataset<a name="line.701"></a>
<span class="sourceLineNo">702</span>         *<a name="line.702"></a>
<span class="sourceLineNo">703</span>         * @param d<a name="line.703"></a>
<span class="sourceLineNo">704</span>         *            The dataset to be copied<a name="line.704"></a>
<span class="sourceLineNo">705</span>         * @param dtype dataset type<a name="line.705"></a>
<span class="sourceLineNo">706</span>         * @return copied dataset of given type<a name="line.706"></a>
<span class="sourceLineNo">707</span>         */<a name="line.707"></a>
<span class="sourceLineNo">708</span>        public static Dataset copy(final IDataset d, final int dtype) {<a name="line.708"></a>
<span class="sourceLineNo">709</span>                Dataset a = convertToDataset(d);<a name="line.709"></a>
<span class="sourceLineNo">710</span><a name="line.710"></a>
<span class="sourceLineNo">711</span>                Dataset c = null;<a name="line.711"></a>
<span class="sourceLineNo">712</span>                try {<a name="line.712"></a>
<span class="sourceLineNo">713</span>                        // copy across the data<a name="line.713"></a>
<span class="sourceLineNo">714</span>                        switch (dtype) {<a name="line.714"></a>
<span class="sourceLineNo">715</span>                        case Dataset.STRING:<a name="line.715"></a>
<span class="sourceLineNo">716</span>                                c = new StringDataset(a);<a name="line.716"></a>
<span class="sourceLineNo">717</span>                                break;<a name="line.717"></a>
<span class="sourceLineNo">718</span>                        case Dataset.BOOL:<a name="line.718"></a>
<span class="sourceLineNo">719</span>                                c = new BooleanDataset(a);<a name="line.719"></a>
<span class="sourceLineNo">720</span>                                break;<a name="line.720"></a>
<span class="sourceLineNo">721</span>                        case Dataset.INT8:<a name="line.721"></a>
<span class="sourceLineNo">722</span>                                if (a instanceof CompoundDataset)<a name="line.722"></a>
<span class="sourceLineNo">723</span>                                        c = new CompoundByteDataset(a);<a name="line.723"></a>
<span class="sourceLineNo">724</span>                                else<a name="line.724"></a>
<span class="sourceLineNo">725</span>                                        c = new ByteDataset(a);<a name="line.725"></a>
<span class="sourceLineNo">726</span>                                break;<a name="line.726"></a>
<span class="sourceLineNo">727</span>                        case Dataset.INT16:<a name="line.727"></a>
<span class="sourceLineNo">728</span>                                if (a instanceof CompoundDataset)<a name="line.728"></a>
<span class="sourceLineNo">729</span>                                        c = new CompoundShortDataset(a);<a name="line.729"></a>
<span class="sourceLineNo">730</span>                                else<a name="line.730"></a>
<span class="sourceLineNo">731</span>                                        c = new ShortDataset(a);<a name="line.731"></a>
<span class="sourceLineNo">732</span>                                break;<a name="line.732"></a>
<span class="sourceLineNo">733</span>                        case Dataset.INT32:<a name="line.733"></a>
<span class="sourceLineNo">734</span>                                if (a instanceof CompoundDataset)<a name="line.734"></a>
<span class="sourceLineNo">735</span>                                        c = new CompoundIntegerDataset(a);<a name="line.735"></a>
<span class="sourceLineNo">736</span>                                else<a name="line.736"></a>
<span class="sourceLineNo">737</span>                                        c = new IntegerDataset(a);<a name="line.737"></a>
<span class="sourceLineNo">738</span>                                break;<a name="line.738"></a>
<span class="sourceLineNo">739</span>                        case Dataset.INT64:<a name="line.739"></a>
<span class="sourceLineNo">740</span>                                if (a instanceof CompoundDataset)<a name="line.740"></a>
<span class="sourceLineNo">741</span>                                        c = new CompoundLongDataset(a);<a name="line.741"></a>
<span class="sourceLineNo">742</span>                                else<a name="line.742"></a>
<span class="sourceLineNo">743</span>                                        c = new LongDataset(a);<a name="line.743"></a>
<span class="sourceLineNo">744</span>                                break;<a name="line.744"></a>
<span class="sourceLineNo">745</span>                        case Dataset.ARRAYINT8:<a name="line.745"></a>
<span class="sourceLineNo">746</span>                                if (a instanceof CompoundDataset)<a name="line.746"></a>
<span class="sourceLineNo">747</span>                                        c = new CompoundByteDataset((CompoundDataset) a);<a name="line.747"></a>
<span class="sourceLineNo">748</span>                                else<a name="line.748"></a>
<span class="sourceLineNo">749</span>                                        c = new CompoundByteDataset(a);<a name="line.749"></a>
<span class="sourceLineNo">750</span>                                break;<a name="line.750"></a>
<span class="sourceLineNo">751</span>                        case Dataset.ARRAYINT16:<a name="line.751"></a>
<span class="sourceLineNo">752</span>                                if (a instanceof CompoundDataset)<a name="line.752"></a>
<span class="sourceLineNo">753</span>                                        c = new CompoundShortDataset((CompoundDataset) a);<a name="line.753"></a>
<span class="sourceLineNo">754</span>                                else<a name="line.754"></a>
<span class="sourceLineNo">755</span>                                        c = new CompoundShortDataset(a);<a name="line.755"></a>
<span class="sourceLineNo">756</span>                                break;<a name="line.756"></a>
<span class="sourceLineNo">757</span>                        case Dataset.ARRAYINT32:<a name="line.757"></a>
<span class="sourceLineNo">758</span>                                if (a instanceof CompoundDataset)<a name="line.758"></a>
<span class="sourceLineNo">759</span>                                        c = new CompoundIntegerDataset((CompoundDataset) a);<a name="line.759"></a>
<span class="sourceLineNo">760</span>                                else<a name="line.760"></a>
<span class="sourceLineNo">761</span>                                        c = new CompoundIntegerDataset(a);<a name="line.761"></a>
<span class="sourceLineNo">762</span>                                break;<a name="line.762"></a>
<span class="sourceLineNo">763</span>                        case Dataset.ARRAYINT64:<a name="line.763"></a>
<span class="sourceLineNo">764</span>                                if (a instanceof CompoundDataset)<a name="line.764"></a>
<span class="sourceLineNo">765</span>                                        c = new CompoundLongDataset((CompoundDataset) a);<a name="line.765"></a>
<span class="sourceLineNo">766</span>                                else<a name="line.766"></a>
<span class="sourceLineNo">767</span>                                        c = new CompoundLongDataset(a);<a name="line.767"></a>
<span class="sourceLineNo">768</span>                                break;<a name="line.768"></a>
<span class="sourceLineNo">769</span>                        case Dataset.FLOAT32:<a name="line.769"></a>
<span class="sourceLineNo">770</span>                                c = new FloatDataset(a);<a name="line.770"></a>
<span class="sourceLineNo">771</span>                                break;<a name="line.771"></a>
<span class="sourceLineNo">772</span>                        case Dataset.FLOAT64:<a name="line.772"></a>
<span class="sourceLineNo">773</span>                                c = new DoubleDataset(a);<a name="line.773"></a>
<span class="sourceLineNo">774</span>                                break;<a name="line.774"></a>
<span class="sourceLineNo">775</span>                        case Dataset.ARRAYFLOAT32:<a name="line.775"></a>
<span class="sourceLineNo">776</span>                                if (a instanceof CompoundDataset)<a name="line.776"></a>
<span class="sourceLineNo">777</span>                                        c = new CompoundFloatDataset((CompoundDataset) a);<a name="line.777"></a>
<span class="sourceLineNo">778</span>                                else<a name="line.778"></a>
<span class="sourceLineNo">779</span>                                        c = new CompoundFloatDataset(a);<a name="line.779"></a>
<span class="sourceLineNo">780</span>                                break;<a name="line.780"></a>
<span class="sourceLineNo">781</span>                        case Dataset.ARRAYFLOAT64:<a name="line.781"></a>
<span class="sourceLineNo">782</span>                                if (a instanceof CompoundDataset)<a name="line.782"></a>
<span class="sourceLineNo">783</span>                                        c = new CompoundDoubleDataset((CompoundDataset) a);<a name="line.783"></a>
<span class="sourceLineNo">784</span>                                else<a name="line.784"></a>
<span class="sourceLineNo">785</span>                                        c = new CompoundDoubleDataset(a);<a name="line.785"></a>
<span class="sourceLineNo">786</span>                                break;<a name="line.786"></a>
<span class="sourceLineNo">787</span>                        case Dataset.COMPLEX64:<a name="line.787"></a>
<span class="sourceLineNo">788</span>                                c = new ComplexFloatDataset(a);<a name="line.788"></a>
<span class="sourceLineNo">789</span>                                break;<a name="line.789"></a>
<span class="sourceLineNo">790</span>                        case Dataset.COMPLEX128:<a name="line.790"></a>
<span class="sourceLineNo">791</span>                                c = new ComplexDoubleDataset(a);<a name="line.791"></a>
<span class="sourceLineNo">792</span>                                break;<a name="line.792"></a>
<span class="sourceLineNo">793</span>                        case Dataset.RGB:<a name="line.793"></a>
<span class="sourceLineNo">794</span>                                if (a instanceof CompoundDataset)<a name="line.794"></a>
<span class="sourceLineNo">795</span>                                        c = RGBDataset.createFromCompoundDataset((CompoundDataset) a);<a name="line.795"></a>
<span class="sourceLineNo">796</span>                                else<a name="line.796"></a>
<span class="sourceLineNo">797</span>                                        c = new RGBDataset(a);<a name="line.797"></a>
<span class="sourceLineNo">798</span>                                break;<a name="line.798"></a>
<span class="sourceLineNo">799</span>                        default:<a name="line.799"></a>
<span class="sourceLineNo">800</span>                                utilsLogger.error("Dataset of unknown type!");<a name="line.800"></a>
<span class="sourceLineNo">801</span>                                break;<a name="line.801"></a>
<span class="sourceLineNo">802</span>                        }<a name="line.802"></a>
<span class="sourceLineNo">803</span>                } catch (OutOfMemoryError e) {<a name="line.803"></a>
<span class="sourceLineNo">804</span>                        utilsLogger.error("Not enough memory available to create dataset");<a name="line.804"></a>
<span class="sourceLineNo">805</span>                        throw new OutOfMemoryError("Not enough memory available to create dataset");<a name="line.805"></a>
<span class="sourceLineNo">806</span>                }<a name="line.806"></a>
<span class="sourceLineNo">807</span><a name="line.807"></a>
<span class="sourceLineNo">808</span>                return c;<a name="line.808"></a>
<span class="sourceLineNo">809</span>        }<a name="line.809"></a>
<span class="sourceLineNo">810</span><a name="line.810"></a>
<span class="sourceLineNo">811</span>        /**<a name="line.811"></a>
<span class="sourceLineNo">812</span>         * Copy and cast a dataset<a name="line.812"></a>
<span class="sourceLineNo">813</span>         *<a name="line.813"></a>
<span class="sourceLineNo">814</span>         * @param clazz dataset class<a name="line.814"></a>
<span class="sourceLineNo">815</span>         * @param d<a name="line.815"></a>
<span class="sourceLineNo">816</span>         *            The dataset to be copied<a name="line.816"></a>
<span class="sourceLineNo">817</span>         * @return copied dataset of given type<a name="line.817"></a>
<span class="sourceLineNo">818</span>         */<a name="line.818"></a>
<span class="sourceLineNo">819</span>        public static &lt;T extends Dataset&gt; T copy(Class&lt;T&gt; clazz, final IDataset d) {<a name="line.819"></a>
<span class="sourceLineNo">820</span>                return (T) copy(d, DTypeUtils.getDType(clazz));<a name="line.820"></a>
<span class="sourceLineNo">821</span>        }<a name="line.821"></a>
<span class="sourceLineNo">822</span><a name="line.822"></a>
<span class="sourceLineNo">823</span><a name="line.823"></a>
<span class="sourceLineNo">824</span>        /**<a name="line.824"></a>
<span class="sourceLineNo">825</span>         * Cast a dataset<a name="line.825"></a>
<span class="sourceLineNo">826</span>         *<a name="line.826"></a>
<span class="sourceLineNo">827</span>         * @param d<a name="line.827"></a>
<span class="sourceLineNo">828</span>         *            The dataset to be cast.<a name="line.828"></a>
<span class="sourceLineNo">829</span>         * @param dtype dataset type<a name="line.829"></a>
<span class="sourceLineNo">830</span>         * @return dataset of given type (or same dataset if already of the right type)<a name="line.830"></a>
<span class="sourceLineNo">831</span>         */<a name="line.831"></a>
<span class="sourceLineNo">832</span>        public static Dataset cast(final IDataset d, final int dtype) {<a name="line.832"></a>
<span class="sourceLineNo">833</span>                Dataset a = convertToDataset(d);<a name="line.833"></a>
<span class="sourceLineNo">834</span><a name="line.834"></a>
<span class="sourceLineNo">835</span>                if (a.getDType() == dtype) {<a name="line.835"></a>
<span class="sourceLineNo">836</span>                        return a;<a name="line.836"></a>
<span class="sourceLineNo">837</span>                }<a name="line.837"></a>
<span class="sourceLineNo">838</span>                return copy(d, dtype);<a name="line.838"></a>
<span class="sourceLineNo">839</span>        }<a name="line.839"></a>
<span class="sourceLineNo">840</span><a name="line.840"></a>
<span class="sourceLineNo">841</span>        /**<a name="line.841"></a>
<span class="sourceLineNo">842</span>         * Cast a dataset<a name="line.842"></a>
<span class="sourceLineNo">843</span>         *<a name="line.843"></a>
<span class="sourceLineNo">844</span>         * @param clazz dataset class<a name="line.844"></a>
<span class="sourceLineNo">845</span>         * @param d<a name="line.845"></a>
<span class="sourceLineNo">846</span>         *            The dataset to be cast.<a name="line.846"></a>
<span class="sourceLineNo">847</span>         * @return dataset of given type (or same dataset if already of the right type)<a name="line.847"></a>
<span class="sourceLineNo">848</span>         */<a name="line.848"></a>
<span class="sourceLineNo">849</span>        public static &lt;T extends Dataset&gt; T cast(Class&lt;T&gt; clazz, final IDataset d) {<a name="line.849"></a>
<span class="sourceLineNo">850</span>                return (T) cast(d, DTypeUtils.getDType(clazz));<a name="line.850"></a>
<span class="sourceLineNo">851</span>        }<a name="line.851"></a>
<span class="sourceLineNo">852</span><a name="line.852"></a>
<span class="sourceLineNo">853</span>        /**<a name="line.853"></a>
<span class="sourceLineNo">854</span>         * Cast a dataset<a name="line.854"></a>
<span class="sourceLineNo">855</span>         *<a name="line.855"></a>
<span class="sourceLineNo">856</span>         * @param d<a name="line.856"></a>
<span class="sourceLineNo">857</span>         *            The dataset to be cast.<a name="line.857"></a>
<span class="sourceLineNo">858</span>         * @param repeat repeat elements over item<a name="line.858"></a>
<span class="sourceLineNo">859</span>         * @param dtype dataset type<a name="line.859"></a>
<span class="sourceLineNo">860</span>         * @param isize item size<a name="line.860"></a>
<span class="sourceLineNo">861</span>         */<a name="line.861"></a>
<span class="sourceLineNo">862</span>        public static Dataset cast(final IDataset d, final boolean repeat, final int dtype, final int isize) {<a name="line.862"></a>
<span class="sourceLineNo">863</span>                Dataset a = convertToDataset(d);<a name="line.863"></a>
<span class="sourceLineNo">864</span><a name="line.864"></a>
<span class="sourceLineNo">865</span>                if (a.getDType() == dtype &amp;&amp; a.getElementsPerItem() == isize) {<a name="line.865"></a>
<span class="sourceLineNo">866</span>                        return a;<a name="line.866"></a>
<span class="sourceLineNo">867</span>                }<a name="line.867"></a>
<span class="sourceLineNo">868</span>                if (isize &lt;= 0) {<a name="line.868"></a>
<span class="sourceLineNo">869</span>                        utilsLogger.error("Item size is invalid (&gt;0)");<a name="line.869"></a>
<span class="sourceLineNo">870</span>                        throw new IllegalArgumentException("Item size is invalid (&gt;0)");<a name="line.870"></a>
<span class="sourceLineNo">871</span>                }<a name="line.871"></a>
<span class="sourceLineNo">872</span>                if (isize &gt; 1 &amp;&amp; dtype &lt;= Dataset.FLOAT64) {<a name="line.872"></a>
<span class="sourceLineNo">873</span>                        utilsLogger.error("Item size is inconsistent with dataset type");<a name="line.873"></a>
<span class="sourceLineNo">874</span>                        throw new IllegalArgumentException("Item size is inconsistent with dataset type");<a name="line.874"></a>
<span class="sourceLineNo">875</span>                }<a name="line.875"></a>
<span class="sourceLineNo">876</span><a name="line.876"></a>
<span class="sourceLineNo">877</span>                Dataset c = null;<a name="line.877"></a>
<span class="sourceLineNo">878</span><a name="line.878"></a>
<span class="sourceLineNo">879</span>                try {<a name="line.879"></a>
<span class="sourceLineNo">880</span>                        // copy across the data<a name="line.880"></a>
<span class="sourceLineNo">881</span>                        switch (dtype) {<a name="line.881"></a>
<span class="sourceLineNo">882</span>                        case Dataset.BOOL:<a name="line.882"></a>
<span class="sourceLineNo">883</span>                                c = new BooleanDataset(a);<a name="line.883"></a>
<span class="sourceLineNo">884</span>                                break;<a name="line.884"></a>
<span class="sourceLineNo">885</span>                        case Dataset.INT8:<a name="line.885"></a>
<span class="sourceLineNo">886</span>                                c = new ByteDataset(a);<a name="line.886"></a>
<span class="sourceLineNo">887</span>                                break;<a name="line.887"></a>
<span class="sourceLineNo">888</span>                        case Dataset.INT16:<a name="line.888"></a>
<span class="sourceLineNo">889</span>                                c = new ShortDataset(a);<a name="line.889"></a>
<span class="sourceLineNo">890</span>                                break;<a name="line.890"></a>
<span class="sourceLineNo">891</span>                        case Dataset.INT32:<a name="line.891"></a>
<span class="sourceLineNo">892</span>                                c = new IntegerDataset(a);<a name="line.892"></a>
<span class="sourceLineNo">893</span>                                break;<a name="line.893"></a>
<span class="sourceLineNo">894</span>                        case Dataset.INT64:<a name="line.894"></a>
<span class="sourceLineNo">895</span>                                c = new LongDataset(a);<a name="line.895"></a>
<span class="sourceLineNo">896</span>                                break;<a name="line.896"></a>
<span class="sourceLineNo">897</span>                        case Dataset.ARRAYINT8:<a name="line.897"></a>
<span class="sourceLineNo">898</span>                                c = new CompoundByteDataset(isize, repeat, a);<a name="line.898"></a>
<span class="sourceLineNo">899</span>                                break;<a name="line.899"></a>
<span class="sourceLineNo">900</span>                        case Dataset.ARRAYINT16:<a name="line.900"></a>
<span class="sourceLineNo">901</span>                                c = new CompoundShortDataset(isize, repeat, a);<a name="line.901"></a>
<span class="sourceLineNo">902</span>                                break;<a name="line.902"></a>
<span class="sourceLineNo">903</span>                        case Dataset.ARRAYINT32:<a name="line.903"></a>
<span class="sourceLineNo">904</span>                                c = new CompoundIntegerDataset(isize, repeat, a);<a name="line.904"></a>
<span class="sourceLineNo">905</span>                                break;<a name="line.905"></a>
<span class="sourceLineNo">906</span>                        case Dataset.ARRAYINT64:<a name="line.906"></a>
<span class="sourceLineNo">907</span>                                c = new CompoundLongDataset(isize, repeat, a);<a name="line.907"></a>
<span class="sourceLineNo">908</span>                                break;<a name="line.908"></a>
<span class="sourceLineNo">909</span>                        case Dataset.FLOAT32:<a name="line.909"></a>
<span class="sourceLineNo">910</span>                                c = new FloatDataset(a);<a name="line.910"></a>
<span class="sourceLineNo">911</span>                                break;<a name="line.911"></a>
<span class="sourceLineNo">912</span>                        case Dataset.FLOAT64:<a name="line.912"></a>
<span class="sourceLineNo">913</span>                                c = new DoubleDataset(a);<a name="line.913"></a>
<span class="sourceLineNo">914</span>                                break;<a name="line.914"></a>
<span class="sourceLineNo">915</span>                        case Dataset.ARRAYFLOAT32:<a name="line.915"></a>
<span class="sourceLineNo">916</span>                                c = new CompoundFloatDataset(isize, repeat, a);<a name="line.916"></a>
<span class="sourceLineNo">917</span>                                break;<a name="line.917"></a>
<span class="sourceLineNo">918</span>                        case Dataset.ARRAYFLOAT64:<a name="line.918"></a>
<span class="sourceLineNo">919</span>                                c = new CompoundDoubleDataset(isize, repeat, a);<a name="line.919"></a>
<span class="sourceLineNo">920</span>                                break;<a name="line.920"></a>
<span class="sourceLineNo">921</span>                        case Dataset.COMPLEX64:<a name="line.921"></a>
<span class="sourceLineNo">922</span>                                c = new ComplexFloatDataset(a);<a name="line.922"></a>
<span class="sourceLineNo">923</span>                                break;<a name="line.923"></a>
<span class="sourceLineNo">924</span>                        case Dataset.COMPLEX128:<a name="line.924"></a>
<span class="sourceLineNo">925</span>                                c = new ComplexDoubleDataset(a);<a name="line.925"></a>
<span class="sourceLineNo">926</span>                                break;<a name="line.926"></a>
<span class="sourceLineNo">927</span>                        default:<a name="line.927"></a>
<span class="sourceLineNo">928</span>                                utilsLogger.error("Dataset of unknown type!");<a name="line.928"></a>
<span class="sourceLineNo">929</span>                                break;<a name="line.929"></a>
<span class="sourceLineNo">930</span>                        }<a name="line.930"></a>
<span class="sourceLineNo">931</span>                } catch (OutOfMemoryError e) {<a name="line.931"></a>
<span class="sourceLineNo">932</span>                        utilsLogger.error("Not enough memory available to create dataset");<a name="line.932"></a>
<span class="sourceLineNo">933</span>                        throw new OutOfMemoryError("Not enough memory available to create dataset");<a name="line.933"></a>
<span class="sourceLineNo">934</span>                }<a name="line.934"></a>
<span class="sourceLineNo">935</span><a name="line.935"></a>
<span class="sourceLineNo">936</span>                return c;<a name="line.936"></a>
<span class="sourceLineNo">937</span>        }<a name="line.937"></a>
<span class="sourceLineNo">938</span><a name="line.938"></a>
<span class="sourceLineNo">939</span>        /**<a name="line.939"></a>
<span class="sourceLineNo">940</span>         * Cast array of datasets to a compound dataset<a name="line.940"></a>
<span class="sourceLineNo">941</span>         *<a name="line.941"></a>
<span class="sourceLineNo">942</span>         * @param a<a name="line.942"></a>
<span class="sourceLineNo">943</span>         *            The datasets to be cast.<a name="line.943"></a>
<span class="sourceLineNo">944</span>         */<a name="line.944"></a>
<span class="sourceLineNo">945</span>        public static CompoundDataset cast(final Dataset[] a, final int dtype) {<a name="line.945"></a>
<span class="sourceLineNo">946</span>                CompoundDataset c = null;<a name="line.946"></a>
<span class="sourceLineNo">947</span><a name="line.947"></a>
<span class="sourceLineNo">948</span>                switch (dtype) {<a name="line.948"></a>
<span class="sourceLineNo">949</span>                case Dataset.INT8:<a name="line.949"></a>
<span class="sourceLineNo">950</span>                case Dataset.ARRAYINT8:<a name="line.950"></a>
<span class="sourceLineNo">951</span>                        c = new CompoundByteDataset(a);<a name="line.951"></a>
<span class="sourceLineNo">952</span>                        break;<a name="line.952"></a>
<span class="sourceLineNo">953</span>                case Dataset.INT16:<a name="line.953"></a>
<span class="sourceLineNo">954</span>                case Dataset.ARRAYINT16:<a name="line.954"></a>
<span class="sourceLineNo">955</span>                        c = new CompoundShortDataset(a);<a name="line.955"></a>
<span class="sourceLineNo">956</span>                        break;<a name="line.956"></a>
<span class="sourceLineNo">957</span>                case Dataset.INT32:<a name="line.957"></a>
<span class="sourceLineNo">958</span>                case Dataset.ARRAYINT32:<a name="line.958"></a>
<span class="sourceLineNo">959</span>                        c = new CompoundIntegerDataset(a);<a name="line.959"></a>
<span class="sourceLineNo">960</span>                        break;<a name="line.960"></a>
<span class="sourceLineNo">961</span>                case Dataset.INT64:<a name="line.961"></a>
<span class="sourceLineNo">962</span>                case Dataset.ARRAYINT64:<a name="line.962"></a>
<span class="sourceLineNo">963</span>                        c = new CompoundLongDataset(a);<a name="line.963"></a>
<span class="sourceLineNo">964</span>                        break;<a name="line.964"></a>
<span class="sourceLineNo">965</span>                case Dataset.FLOAT32:<a name="line.965"></a>
<span class="sourceLineNo">966</span>                case Dataset.ARRAYFLOAT32:<a name="line.966"></a>
<span class="sourceLineNo">967</span>                        c = new CompoundFloatDataset(a);<a name="line.967"></a>
<span class="sourceLineNo">968</span>                        break;<a name="line.968"></a>
<span class="sourceLineNo">969</span>                case Dataset.FLOAT64:<a name="line.969"></a>
<span class="sourceLineNo">970</span>                case Dataset.ARRAYFLOAT64:<a name="line.970"></a>
<span class="sourceLineNo">971</span>                        c = new CompoundDoubleDataset(a);<a name="line.971"></a>
<span class="sourceLineNo">972</span>                        break;<a name="line.972"></a>
<span class="sourceLineNo">973</span>                case Dataset.COMPLEX64:<a name="line.973"></a>
<span class="sourceLineNo">974</span>                        if (a.length != 2) {<a name="line.974"></a>
<span class="sourceLineNo">975</span>                                throw new IllegalArgumentException("Need two datasets for complex dataset type");<a name="line.975"></a>
<span class="sourceLineNo">976</span>                        }<a name="line.976"></a>
<span class="sourceLineNo">977</span>                        c = new ComplexFloatDataset(a[0], a[1]);<a name="line.977"></a>
<span class="sourceLineNo">978</span>                        break;<a name="line.978"></a>
<span class="sourceLineNo">979</span>                case Dataset.COMPLEX128:<a name="line.979"></a>
<span class="sourceLineNo">980</span>                        if (a.length != 2) {<a name="line.980"></a>
<span class="sourceLineNo">981</span>                                throw new IllegalArgumentException("Need two datasets for complex dataset type");<a name="line.981"></a>
<span class="sourceLineNo">982</span>                        }<a name="line.982"></a>
<span class="sourceLineNo">983</span>                        c = new ComplexDoubleDataset(a[0], a[1]);<a name="line.983"></a>
<span class="sourceLineNo">984</span>                        break;<a name="line.984"></a>
<span class="sourceLineNo">985</span>                default:<a name="line.985"></a>
<span class="sourceLineNo">986</span>                        utilsLogger.error("Dataset of unsupported type!");<a name="line.986"></a>
<span class="sourceLineNo">987</span>                        break;<a name="line.987"></a>
<span class="sourceLineNo">988</span>                }<a name="line.988"></a>
<span class="sourceLineNo">989</span><a name="line.989"></a>
<span class="sourceLineNo">990</span>                return c;<a name="line.990"></a>
<span class="sourceLineNo">991</span>        }<a name="line.991"></a>
<span class="sourceLineNo">992</span><a name="line.992"></a>
<span class="sourceLineNo">993</span>        /**<a name="line.993"></a>
<span class="sourceLineNo">994</span>         * Make a dataset unsigned by promoting it to a wider dataset type and unwrapping the signs<a name="line.994"></a>
<span class="sourceLineNo">995</span>         * of its contents<a name="line.995"></a>
<span class="sourceLineNo">996</span>         * @param a<a name="line.996"></a>
<span class="sourceLineNo">997</span>         * @return unsigned dataset or original if it is not an integer dataset<a name="line.997"></a>
<span class="sourceLineNo">998</span>         */<a name="line.998"></a>
<span class="sourceLineNo">999</span>        public static Dataset makeUnsigned(IDataset a) {<a name="line.999"></a>
<span class="sourceLineNo">1000</span>                return makeUnsigned(a, false);<a name="line.1000"></a>
<span class="sourceLineNo">1001</span>        }<a name="line.1001"></a>
<span class="sourceLineNo">1002</span><a name="line.1002"></a>
<span class="sourceLineNo">1003</span>        /**<a name="line.1003"></a>
<span class="sourceLineNo">1004</span>         * Make a dataset unsigned by promoting it to a wider dataset type and unwrapping the signs<a name="line.1004"></a>
<span class="sourceLineNo">1005</span>         * of its contents<a name="line.1005"></a>
<span class="sourceLineNo">1006</span>         * @param a<a name="line.1006"></a>
<span class="sourceLineNo">1007</span>         * @param check if true, then check for negative values<a name="line.1007"></a>
<span class="sourceLineNo">1008</span>         * @return unsigned dataset or original if it is not an integer dataset or it has been check for negative numbers<a name="line.1008"></a>
<span class="sourceLineNo">1009</span>         * @since 2.1<a name="line.1009"></a>
<span class="sourceLineNo">1010</span>         */<a name="line.1010"></a>
<span class="sourceLineNo">1011</span>        public static Dataset makeUnsigned(IDataset a, boolean check) {<a name="line.1011"></a>
<span class="sourceLineNo">1012</span>                Dataset d = convertToDataset(a);<a name="line.1012"></a>
<span class="sourceLineNo">1013</span><a name="line.1013"></a>
<span class="sourceLineNo">1014</span>                if (d.hasFloatingPointElements()) {<a name="line.1014"></a>
<span class="sourceLineNo">1015</span>                        return d;<a name="line.1015"></a>
<span class="sourceLineNo">1016</span>                }<a name="line.1016"></a>
<span class="sourceLineNo">1017</span>                if (check &amp;&amp; d.min(true).longValue() &gt;= 0) {<a name="line.1017"></a>
<span class="sourceLineNo">1018</span>                        return d;<a name="line.1018"></a>
<span class="sourceLineNo">1019</span>                }<a name="line.1019"></a>
<span class="sourceLineNo">1020</span><a name="line.1020"></a>
<span class="sourceLineNo">1021</span>                int dtype = d.getDType();<a name="line.1021"></a>
<span class="sourceLineNo">1022</span>                switch (dtype) {<a name="line.1022"></a>
<span class="sourceLineNo">1023</span>                case Dataset.INT32:<a name="line.1023"></a>
<span class="sourceLineNo">1024</span>                        d = new LongDataset(d);<a name="line.1024"></a>
<span class="sourceLineNo">1025</span>                        unwrapUnsigned(d, 32);<a name="line.1025"></a>
<span class="sourceLineNo">1026</span>                        break;<a name="line.1026"></a>
<span class="sourceLineNo">1027</span>                case Dataset.INT16:<a name="line.1027"></a>
<span class="sourceLineNo">1028</span>                        d = new IntegerDataset(d);<a name="line.1028"></a>
<span class="sourceLineNo">1029</span>                        unwrapUnsigned(d, 16);<a name="line.1029"></a>
<span class="sourceLineNo">1030</span>                        break;<a name="line.1030"></a>
<span class="sourceLineNo">1031</span>                case Dataset.INT8:<a name="line.1031"></a>
<span class="sourceLineNo">1032</span>                        d = new ShortDataset(d);<a name="line.1032"></a>
<span class="sourceLineNo">1033</span>                        unwrapUnsigned(d, 8);<a name="line.1033"></a>
<span class="sourceLineNo">1034</span>                        break;<a name="line.1034"></a>
<span class="sourceLineNo">1035</span>                case Dataset.ARRAYINT32:<a name="line.1035"></a>
<span class="sourceLineNo">1036</span>                        d = new CompoundLongDataset(d);<a name="line.1036"></a>
<span class="sourceLineNo">1037</span>                        unwrapUnsigned(d, 32);<a name="line.1037"></a>
<span class="sourceLineNo">1038</span>                        break;<a name="line.1038"></a>
<span class="sourceLineNo">1039</span>                case Dataset.ARRAYINT16:<a name="line.1039"></a>
<span class="sourceLineNo">1040</span>                        d = new CompoundIntegerDataset(d);<a name="line.1040"></a>
<span class="sourceLineNo">1041</span>                        unwrapUnsigned(d, 16);<a name="line.1041"></a>
<span class="sourceLineNo">1042</span>                        break;<a name="line.1042"></a>
<span class="sourceLineNo">1043</span>                case Dataset.ARRAYINT8:<a name="line.1043"></a>
<span class="sourceLineNo">1044</span>                        d = new CompoundShortDataset(d);<a name="line.1044"></a>
<span class="sourceLineNo">1045</span>                        unwrapUnsigned(d, 8);<a name="line.1045"></a>
<span class="sourceLineNo">1046</span>                        break;<a name="line.1046"></a>
<span class="sourceLineNo">1047</span>                }<a name="line.1047"></a>
<span class="sourceLineNo">1048</span>                return d;<a name="line.1048"></a>
<span class="sourceLineNo">1049</span>        }<a name="line.1049"></a>
<span class="sourceLineNo">1050</span><a name="line.1050"></a>
<span class="sourceLineNo">1051</span>        /**<a name="line.1051"></a>
<span class="sourceLineNo">1052</span>         * Unwrap dataset elements so that all elements are unsigned<a name="line.1052"></a>
<span class="sourceLineNo">1053</span>         * @param a dataset<a name="line.1053"></a>
<span class="sourceLineNo">1054</span>         * @param bitWidth width of original primitive in bits<a name="line.1054"></a>
<span class="sourceLineNo">1055</span>         */<a name="line.1055"></a>
<span class="sourceLineNo">1056</span>        public static void unwrapUnsigned(Dataset a, final int bitWidth) {<a name="line.1056"></a>
<span class="sourceLineNo">1057</span>                final int dtype = a.getDType();<a name="line.1057"></a>
<span class="sourceLineNo">1058</span>                final double dv = 1L &lt;&lt; bitWidth;<a name="line.1058"></a>
<span class="sourceLineNo">1059</span>                final int isize = a.getElementsPerItem();<a name="line.1059"></a>
<span class="sourceLineNo">1060</span>                IndexIterator it = a.getIterator();<a name="line.1060"></a>
<span class="sourceLineNo">1061</span><a name="line.1061"></a>
<span class="sourceLineNo">1062</span>                switch (dtype) {<a name="line.1062"></a>
<span class="sourceLineNo">1063</span>                case Dataset.BOOL:<a name="line.1063"></a>
<span class="sourceLineNo">1064</span>                        break;<a name="line.1064"></a>
<span class="sourceLineNo">1065</span>                case Dataset.INT8:<a name="line.1065"></a>
<span class="sourceLineNo">1066</span>                        break;<a name="line.1066"></a>
<span class="sourceLineNo">1067</span>                case Dataset.INT16:<a name="line.1067"></a>
<span class="sourceLineNo">1068</span>                        ShortDataset sds = (ShortDataset) a;<a name="line.1068"></a>
<span class="sourceLineNo">1069</span>                        final short soffset = (short) dv;<a name="line.1069"></a>
<span class="sourceLineNo">1070</span>                        while (it.hasNext()) {<a name="line.1070"></a>
<span class="sourceLineNo">1071</span>                                final short x = sds.getAbs(it.index);<a name="line.1071"></a>
<span class="sourceLineNo">1072</span>                                if (x &lt; 0)<a name="line.1072"></a>
<span class="sourceLineNo">1073</span>                                        sds.setAbs(it.index, (short) (x + soffset));<a name="line.1073"></a>
<span class="sourceLineNo">1074</span>                        }<a name="line.1074"></a>
<span class="sourceLineNo">1075</span>                        break;<a name="line.1075"></a>
<span class="sourceLineNo">1076</span>                case Dataset.INT32:<a name="line.1076"></a>
<span class="sourceLineNo">1077</span>                        IntegerDataset ids = (IntegerDataset) a;<a name="line.1077"></a>
<span class="sourceLineNo">1078</span>                        final int ioffset = (int) dv;<a name="line.1078"></a>
<span class="sourceLineNo">1079</span>                        while (it.hasNext()) {<a name="line.1079"></a>
<span class="sourceLineNo">1080</span>                                final int x = ids.getAbs(it.index);<a name="line.1080"></a>
<span class="sourceLineNo">1081</span>                                if (x &lt; 0)<a name="line.1081"></a>
<span class="sourceLineNo">1082</span>                                        ids.setAbs(it.index, x + ioffset);<a name="line.1082"></a>
<span class="sourceLineNo">1083</span>                        }<a name="line.1083"></a>
<span class="sourceLineNo">1084</span>                        break;<a name="line.1084"></a>
<span class="sourceLineNo">1085</span>                case Dataset.INT64:<a name="line.1085"></a>
<span class="sourceLineNo">1086</span>                        LongDataset lds = (LongDataset) a;<a name="line.1086"></a>
<span class="sourceLineNo">1087</span>                        final long loffset = (long) dv;<a name="line.1087"></a>
<span class="sourceLineNo">1088</span>                        while (it.hasNext()) {<a name="line.1088"></a>
<span class="sourceLineNo">1089</span>                                final long x = lds.getAbs(it.index);<a name="line.1089"></a>
<span class="sourceLineNo">1090</span>                                if (x &lt; 0)<a name="line.1090"></a>
<span class="sourceLineNo">1091</span>                                        lds.setAbs(it.index, x + loffset);<a name="line.1091"></a>
<span class="sourceLineNo">1092</span>                        }<a name="line.1092"></a>
<span class="sourceLineNo">1093</span>                        break;<a name="line.1093"></a>
<span class="sourceLineNo">1094</span>                case Dataset.FLOAT32:<a name="line.1094"></a>
<span class="sourceLineNo">1095</span>                        FloatDataset fds = (FloatDataset) a;<a name="line.1095"></a>
<span class="sourceLineNo">1096</span>                        final float foffset = (float) dv;<a name="line.1096"></a>
<span class="sourceLineNo">1097</span>                        while (it.hasNext()) {<a name="line.1097"></a>
<span class="sourceLineNo">1098</span>                                final float x = fds.getAbs(it.index);<a name="line.1098"></a>
<span class="sourceLineNo">1099</span>                                if (x &lt; 0)<a name="line.1099"></a>
<span class="sourceLineNo">1100</span>                                        fds.setAbs(it.index, x + foffset);<a name="line.1100"></a>
<span class="sourceLineNo">1101</span>                        }<a name="line.1101"></a>
<span class="sourceLineNo">1102</span>                        break;<a name="line.1102"></a>
<span class="sourceLineNo">1103</span>                case Dataset.FLOAT64:<a name="line.1103"></a>
<span class="sourceLineNo">1104</span>                        DoubleDataset dds = (DoubleDataset) a;<a name="line.1104"></a>
<span class="sourceLineNo">1105</span>                        final double doffset = dv;<a name="line.1105"></a>
<span class="sourceLineNo">1106</span>                        while (it.hasNext()) {<a name="line.1106"></a>
<span class="sourceLineNo">1107</span>                                final double x = dds.getAbs(it.index);<a name="line.1107"></a>
<span class="sourceLineNo">1108</span>                                if (x &lt; 0)<a name="line.1108"></a>
<span class="sourceLineNo">1109</span>                                        dds.setAbs(it.index, x + doffset);<a name="line.1109"></a>
<span class="sourceLineNo">1110</span>                        }<a name="line.1110"></a>
<span class="sourceLineNo">1111</span>                        break;<a name="line.1111"></a>
<span class="sourceLineNo">1112</span>                case Dataset.ARRAYINT8:<a name="line.1112"></a>
<span class="sourceLineNo">1113</span>                        break;<a name="line.1113"></a>
<span class="sourceLineNo">1114</span>                case Dataset.ARRAYINT16:<a name="line.1114"></a>
<span class="sourceLineNo">1115</span>                        CompoundShortDataset csds = (CompoundShortDataset) a;<a name="line.1115"></a>
<span class="sourceLineNo">1116</span>                        final short csoffset = (short) dv;<a name="line.1116"></a>
<span class="sourceLineNo">1117</span>                        final short[] csa = new short[isize];<a name="line.1117"></a>
<span class="sourceLineNo">1118</span>                        while (it.hasNext()) {<a name="line.1118"></a>
<span class="sourceLineNo">1119</span>                                csds.getAbs(it.index, csa);<a name="line.1119"></a>
<span class="sourceLineNo">1120</span>                                boolean dirty = false;<a name="line.1120"></a>
<span class="sourceLineNo">1121</span>                                for (int i = 0; i &lt; isize; i++) {<a name="line.1121"></a>
<span class="sourceLineNo">1122</span>                                        short x = csa[i];<a name="line.1122"></a>
<span class="sourceLineNo">1123</span>                                        if (x &lt; 0) {<a name="line.1123"></a>
<span class="sourceLineNo">1124</span>                                                csa[i] = (short) (x + csoffset);<a name="line.1124"></a>
<span class="sourceLineNo">1125</span>                                                dirty = true;<a name="line.1125"></a>
<span class="sourceLineNo">1126</span>                                        }<a name="line.1126"></a>
<span class="sourceLineNo">1127</span>                                }<a name="line.1127"></a>
<span class="sourceLineNo">1128</span>                                if (dirty)<a name="line.1128"></a>
<span class="sourceLineNo">1129</span>                                        csds.setAbs(it.index, csa);<a name="line.1129"></a>
<span class="sourceLineNo">1130</span>                        }<a name="line.1130"></a>
<span class="sourceLineNo">1131</span>                        break;<a name="line.1131"></a>
<span class="sourceLineNo">1132</span>                case Dataset.ARRAYINT32:<a name="line.1132"></a>
<span class="sourceLineNo">1133</span>                        CompoundIntegerDataset cids = (CompoundIntegerDataset) a;<a name="line.1133"></a>
<span class="sourceLineNo">1134</span>                        final int cioffset = (int) dv;<a name="line.1134"></a>
<span class="sourceLineNo">1135</span>                        final int[] cia = new int[isize];<a name="line.1135"></a>
<span class="sourceLineNo">1136</span>                        while (it.hasNext()) {<a name="line.1136"></a>
<span class="sourceLineNo">1137</span>                                cids.getAbs(it.index, cia);<a name="line.1137"></a>
<span class="sourceLineNo">1138</span>                                boolean dirty = false;<a name="line.1138"></a>
<span class="sourceLineNo">1139</span>                                for (int i = 0; i &lt; isize; i++) {<a name="line.1139"></a>
<span class="sourceLineNo">1140</span>                                        int x = cia[i];<a name="line.1140"></a>
<span class="sourceLineNo">1141</span>                                        if (x &lt; 0) {<a name="line.1141"></a>
<span class="sourceLineNo">1142</span>                                                cia[i] = x + cioffset;<a name="line.1142"></a>
<span class="sourceLineNo">1143</span>                                                dirty = true;<a name="line.1143"></a>
<span class="sourceLineNo">1144</span>                                        }<a name="line.1144"></a>
<span class="sourceLineNo">1145</span>                                }<a name="line.1145"></a>
<span class="sourceLineNo">1146</span>                                if (dirty)<a name="line.1146"></a>
<span class="sourceLineNo">1147</span>                                        cids.setAbs(it.index, cia);<a name="line.1147"></a>
<span class="sourceLineNo">1148</span>                        }<a name="line.1148"></a>
<span class="sourceLineNo">1149</span>                        break;<a name="line.1149"></a>
<span class="sourceLineNo">1150</span>                case Dataset.ARRAYINT64:<a name="line.1150"></a>
<span class="sourceLineNo">1151</span>                        CompoundLongDataset clds = (CompoundLongDataset) a;<a name="line.1151"></a>
<span class="sourceLineNo">1152</span>                        final long cloffset = (long) dv;<a name="line.1152"></a>
<span class="sourceLineNo">1153</span>                        final long[] cla = new long[isize];<a name="line.1153"></a>
<span class="sourceLineNo">1154</span>                        while (it.hasNext()) {<a name="line.1154"></a>
<span class="sourceLineNo">1155</span>                                clds.getAbs(it.index, cla);<a name="line.1155"></a>
<span class="sourceLineNo">1156</span>                                boolean dirty = false;<a name="line.1156"></a>
<span class="sourceLineNo">1157</span>                                for (int i = 0; i &lt; isize; i++) {<a name="line.1157"></a>
<span class="sourceLineNo">1158</span>                                        long x = cla[i];<a name="line.1158"></a>
<span class="sourceLineNo">1159</span>                                        if (x &lt; 0) {<a name="line.1159"></a>
<span class="sourceLineNo">1160</span>                                                cla[i] = x + cloffset;<a name="line.1160"></a>
<span class="sourceLineNo">1161</span>                                                dirty = true;<a name="line.1161"></a>
<span class="sourceLineNo">1162</span>                                        }<a name="line.1162"></a>
<span class="sourceLineNo">1163</span>                                }<a name="line.1163"></a>
<span class="sourceLineNo">1164</span>                                if (dirty)<a name="line.1164"></a>
<span class="sourceLineNo">1165</span>                                        clds.setAbs(it.index, cla);<a name="line.1165"></a>
<span class="sourceLineNo">1166</span>                        }<a name="line.1166"></a>
<span class="sourceLineNo">1167</span>                        break;<a name="line.1167"></a>
<span class="sourceLineNo">1168</span>                default:<a name="line.1168"></a>
<span class="sourceLineNo">1169</span>                        utilsLogger.error("Dataset of unsupported type for this method");<a name="line.1169"></a>
<span class="sourceLineNo">1170</span>                        break;<a name="line.1170"></a>
<span class="sourceLineNo">1171</span>                }<a name="line.1171"></a>
<span class="sourceLineNo">1172</span>        }<a name="line.1172"></a>
<span class="sourceLineNo">1173</span><a name="line.1173"></a>
<span class="sourceLineNo">1174</span>        /**<a name="line.1174"></a>
<span class="sourceLineNo">1175</span>         * @param rows<a name="line.1175"></a>
<span class="sourceLineNo">1176</span>         * @param cols<a name="line.1176"></a>
<span class="sourceLineNo">1177</span>         * @param offset<a name="line.1177"></a>
<span class="sourceLineNo">1178</span>         * @param dtype<a name="line.1178"></a>
<span class="sourceLineNo">1179</span>         * @return a new 2d dataset of given shape and type, filled with ones on the (offset) diagonal<a name="line.1179"></a>
<span class="sourceLineNo">1180</span>         */<a name="line.1180"></a>
<span class="sourceLineNo">1181</span>        public static Dataset eye(final int rows, final int cols, final int offset, final int dtype) {<a name="line.1181"></a>
<span class="sourceLineNo">1182</span>                int[] shape = new int[] {rows, cols};<a name="line.1182"></a>
<span class="sourceLineNo">1183</span>                @SuppressWarnings("deprecation")<a name="line.1183"></a>
<span class="sourceLineNo">1184</span>                Dataset a = DatasetFactory.zeros(shape, dtype);<a name="line.1184"></a>
<span class="sourceLineNo">1185</span><a name="line.1185"></a>
<span class="sourceLineNo">1186</span>                int[] pos = new int[] {0, offset};<a name="line.1186"></a>
<span class="sourceLineNo">1187</span>                while (pos[1] &lt; 0) {<a name="line.1187"></a>
<span class="sourceLineNo">1188</span>                        pos[0]++;<a name="line.1188"></a>
<span class="sourceLineNo">1189</span>                        pos[1]++;<a name="line.1189"></a>
<span class="sourceLineNo">1190</span>                }<a name="line.1190"></a>
<span class="sourceLineNo">1191</span>                while (pos[0] &lt; rows &amp;&amp; pos[1] &lt; cols) {<a name="line.1191"></a>
<span class="sourceLineNo">1192</span>                        a.set(1, pos);<a name="line.1192"></a>
<span class="sourceLineNo">1193</span>                        pos[0]++;<a name="line.1193"></a>
<span class="sourceLineNo">1194</span>                        pos[1]++;<a name="line.1194"></a>
<span class="sourceLineNo">1195</span>                }<a name="line.1195"></a>
<span class="sourceLineNo">1196</span><a name="line.1196"></a>
<span class="sourceLineNo">1197</span>                return a;<a name="line.1197"></a>
<span class="sourceLineNo">1198</span>        }<a name="line.1198"></a>
<span class="sourceLineNo">1199</span><a name="line.1199"></a>
<span class="sourceLineNo">1200</span>        /**<a name="line.1200"></a>
<span class="sourceLineNo">1201</span>         * Create a (off-)diagonal matrix from items in dataset<a name="line.1201"></a>
<span class="sourceLineNo">1202</span>         * @param a<a name="line.1202"></a>
<span class="sourceLineNo">1203</span>         * @param offset<a name="line.1203"></a>
<span class="sourceLineNo">1204</span>         * @return diagonal matrix<a name="line.1204"></a>
<span class="sourceLineNo">1205</span>         */<a name="line.1205"></a>
<span class="sourceLineNo">1206</span>        @SuppressWarnings("deprecation")<a name="line.1206"></a>
<span class="sourceLineNo">1207</span>        public static &lt;T extends Dataset&gt; T diag(final T a, final int offset) {<a name="line.1207"></a>
<span class="sourceLineNo">1208</span>                final int dtype = a.getDType();<a name="line.1208"></a>
<span class="sourceLineNo">1209</span>                final int rank = a.getRank();<a name="line.1209"></a>
<span class="sourceLineNo">1210</span>                final int is = a.getElementsPerItem();<a name="line.1210"></a>
<span class="sourceLineNo">1211</span><a name="line.1211"></a>
<span class="sourceLineNo">1212</span>                if (rank == 0 || rank &gt; 2) {<a name="line.1212"></a>
<span class="sourceLineNo">1213</span>                        utilsLogger.error("Rank of dataset should be one or two");<a name="line.1213"></a>
<span class="sourceLineNo">1214</span>                        throw new IllegalArgumentException("Rank of dataset should be one or two");<a name="line.1214"></a>
<span class="sourceLineNo">1215</span>                }<a name="line.1215"></a>
<span class="sourceLineNo">1216</span><a name="line.1216"></a>
<span class="sourceLineNo">1217</span>                Dataset result;<a name="line.1217"></a>
<span class="sourceLineNo">1218</span>                final int[] shape = a.getShapeRef();<a name="line.1218"></a>
<span class="sourceLineNo">1219</span>                if (rank == 1) {<a name="line.1219"></a>
<span class="sourceLineNo">1220</span>                        int side = shape[0] + Math.abs(offset);<a name="line.1220"></a>
<span class="sourceLineNo">1221</span>                        int[] pos = new int[] {side, side};<a name="line.1221"></a>
<span class="sourceLineNo">1222</span>                        result = DatasetFactory.zeros(is, pos, dtype);<a name="line.1222"></a>
<span class="sourceLineNo">1223</span>                        if (offset &gt;= 0) {<a name="line.1223"></a>
<span class="sourceLineNo">1224</span>                                pos[0] = 0;<a name="line.1224"></a>
<span class="sourceLineNo">1225</span>                                pos[1] = offset;<a name="line.1225"></a>
<span class="sourceLineNo">1226</span>                        } else {<a name="line.1226"></a>
<span class="sourceLineNo">1227</span>                                pos[0] = -offset;<a name="line.1227"></a>
<span class="sourceLineNo">1228</span>                                pos[1] = 0;<a name="line.1228"></a>
<span class="sourceLineNo">1229</span>                        }<a name="line.1229"></a>
<span class="sourceLineNo">1230</span>                        int i = 0;<a name="line.1230"></a>
<span class="sourceLineNo">1231</span>                        while (pos[0] &lt; side &amp;&amp; pos[1] &lt; side) {<a name="line.1231"></a>
<span class="sourceLineNo">1232</span>                                result.set(a.getObject(i++), pos);<a name="line.1232"></a>
<span class="sourceLineNo">1233</span>                                pos[0]++;<a name="line.1233"></a>
<span class="sourceLineNo">1234</span>                                pos[1]++;<a name="line.1234"></a>
<span class="sourceLineNo">1235</span>                        }<a name="line.1235"></a>
<span class="sourceLineNo">1236</span>                } else {<a name="line.1236"></a>
<span class="sourceLineNo">1237</span>                        int side = offset &gt;= 0 ? Math.min(shape[0], shape[1]-offset) : Math.min(shape[0]+offset, shape[1]);<a name="line.1237"></a>
<span class="sourceLineNo">1238</span>                        if (side &lt; 0)<a name="line.1238"></a>
<span class="sourceLineNo">1239</span>                                side = 0;<a name="line.1239"></a>
<span class="sourceLineNo">1240</span>                        result = DatasetFactory.zeros(is, new int[] {side}, dtype);<a name="line.1240"></a>
<span class="sourceLineNo">1241</span><a name="line.1241"></a>
<span class="sourceLineNo">1242</span>                        if (side &gt; 0) {<a name="line.1242"></a>
<span class="sourceLineNo">1243</span>                                int[] pos = offset &gt;= 0 ? new int[] { 0, offset } : new int[] { -offset, 0 };<a name="line.1243"></a>
<span class="sourceLineNo">1244</span>                                int i = 0;<a name="line.1244"></a>
<span class="sourceLineNo">1245</span>                                while (pos[0] &lt; shape[0] &amp;&amp; pos[1] &lt; shape[1]) {<a name="line.1245"></a>
<span class="sourceLineNo">1246</span>                                        result.set(a.getObject(pos), i++);<a name="line.1246"></a>
<span class="sourceLineNo">1247</span>                                        pos[0]++;<a name="line.1247"></a>
<span class="sourceLineNo">1248</span>                                        pos[1]++;<a name="line.1248"></a>
<span class="sourceLineNo">1249</span>                                }<a name="line.1249"></a>
<span class="sourceLineNo">1250</span>                        }<a name="line.1250"></a>
<span class="sourceLineNo">1251</span>                }<a name="line.1251"></a>
<span class="sourceLineNo">1252</span><a name="line.1252"></a>
<span class="sourceLineNo">1253</span>                return (T) result;<a name="line.1253"></a>
<span class="sourceLineNo">1254</span>        }<a name="line.1254"></a>
<span class="sourceLineNo">1255</span><a name="line.1255"></a>
<span class="sourceLineNo">1256</span>        /**<a name="line.1256"></a>
<span class="sourceLineNo">1257</span>         * Slice (or fully load), if necessary, a lazy dataset, otherwise take a slice view and<a name="line.1257"></a>
<span class="sourceLineNo">1258</span>         * convert to our dataset implementation. If a slice is necessary, this may cause resource<a name="line.1258"></a>
<span class="sourceLineNo">1259</span>         * problems when used on large datasets and throw runtime exceptions<a name="line.1259"></a>
<span class="sourceLineNo">1260</span>         * @param lazy can be null<a name="line.1260"></a>
<span class="sourceLineNo">1261</span>         * @return Converted dataset or null<a name="line.1261"></a>
<span class="sourceLineNo">1262</span>         * @throws DatasetException<a name="line.1262"></a>
<span class="sourceLineNo">1263</span>         */<a name="line.1263"></a>
<span class="sourceLineNo">1264</span>        public static Dataset sliceAndConvertLazyDataset(ILazyDataset lazy) throws DatasetException {<a name="line.1264"></a>
<span class="sourceLineNo">1265</span>                if (lazy == null)<a name="line.1265"></a>
<span class="sourceLineNo">1266</span>                        return null;<a name="line.1266"></a>
<span class="sourceLineNo">1267</span><a name="line.1267"></a>
<span class="sourceLineNo">1268</span>                IDataset data = lazy instanceof IDataset ? (IDataset) lazy.getSliceView() : lazy.getSlice();<a name="line.1268"></a>
<span class="sourceLineNo">1269</span><a name="line.1269"></a>
<span class="sourceLineNo">1270</span>                return convertToDataset(data);<a name="line.1270"></a>
<span class="sourceLineNo">1271</span>        }<a name="line.1271"></a>
<span class="sourceLineNo">1272</span><a name="line.1272"></a>
<span class="sourceLineNo">1273</span>        /**<a name="line.1273"></a>
<span class="sourceLineNo">1274</span>         * Convert (if necessary) a dataset obeying the interface to our implementation<a name="line.1274"></a>
<span class="sourceLineNo">1275</span>         * @param data can be null<a name="line.1275"></a>
<span class="sourceLineNo">1276</span>         * @return Converted dataset or null<a name="line.1276"></a>
<span class="sourceLineNo">1277</span>         */<a name="line.1277"></a>
<span class="sourceLineNo">1278</span>        public static Dataset convertToDataset(IDataset data) {<a name="line.1278"></a>
<span class="sourceLineNo">1279</span>                if (data == null)<a name="line.1279"></a>
<span class="sourceLineNo">1280</span>                        return null;<a name="line.1280"></a>
<span class="sourceLineNo">1281</span><a name="line.1281"></a>
<span class="sourceLineNo">1282</span>                if (data instanceof Dataset) {<a name="line.1282"></a>
<span class="sourceLineNo">1283</span>                        return (Dataset) data;<a name="line.1283"></a>
<span class="sourceLineNo">1284</span>                }<a name="line.1284"></a>
<span class="sourceLineNo">1285</span><a name="line.1285"></a>
<span class="sourceLineNo">1286</span>                int dtype = DTypeUtils.getDType(data);<a name="line.1286"></a>
<span class="sourceLineNo">1287</span><a name="line.1287"></a>
<span class="sourceLineNo">1288</span>                final int isize = data.getElementsPerItem();<a name="line.1288"></a>
<span class="sourceLineNo">1289</span>                if (isize &lt;= 0) {<a name="line.1289"></a>
<span class="sourceLineNo">1290</span>                        throw new IllegalArgumentException("Datasets with " + isize + " elements per item not supported");<a name="line.1290"></a>
<span class="sourceLineNo">1291</span>                }<a name="line.1291"></a>
<span class="sourceLineNo">1292</span><a name="line.1292"></a>
<span class="sourceLineNo">1293</span>                @SuppressWarnings("deprecation")<a name="line.1293"></a>
<span class="sourceLineNo">1294</span>                final Dataset result = DatasetFactory.zeros(isize, data.getShape(), dtype);<a name="line.1294"></a>
<span class="sourceLineNo">1295</span>                result.setName(data.getName());<a name="line.1295"></a>
<span class="sourceLineNo">1296</span><a name="line.1296"></a>
<span class="sourceLineNo">1297</span>                final IndexIterator it = result.getIterator(true);<a name="line.1297"></a>
<span class="sourceLineNo">1298</span>                final int[] pos = it.getPos();<a name="line.1298"></a>
<span class="sourceLineNo">1299</span>                switch (dtype) {<a name="line.1299"></a>
<span class="sourceLineNo">1300</span>                case Dataset.BOOL:<a name="line.1300"></a>
<span class="sourceLineNo">1301</span>                        while (it.hasNext()) {<a name="line.1301"></a>
<span class="sourceLineNo">1302</span>                                result.setObjectAbs(it.index, data.getBoolean(pos));<a name="line.1302"></a>
<span class="sourceLineNo">1303</span>                        }<a name="line.1303"></a>
<span class="sourceLineNo">1304</span>                        break;<a name="line.1304"></a>
<span class="sourceLineNo">1305</span>                case Dataset.INT8:<a name="line.1305"></a>
<span class="sourceLineNo">1306</span>                        while (it.hasNext()) {<a name="line.1306"></a>
<span class="sourceLineNo">1307</span>                                result.setObjectAbs(it.index, data.getByte(pos));<a name="line.1307"></a>
<span class="sourceLineNo">1308</span>                        }<a name="line.1308"></a>
<span class="sourceLineNo">1309</span>                        break;<a name="line.1309"></a>
<span class="sourceLineNo">1310</span>                case Dataset.INT16:<a name="line.1310"></a>
<span class="sourceLineNo">1311</span>                        while (it.hasNext()) {<a name="line.1311"></a>
<span class="sourceLineNo">1312</span>                                result.setObjectAbs(it.index, data.getShort(pos));<a name="line.1312"></a>
<span class="sourceLineNo">1313</span>                        }<a name="line.1313"></a>
<span class="sourceLineNo">1314</span>                        break;<a name="line.1314"></a>
<span class="sourceLineNo">1315</span>                case Dataset.INT32:<a name="line.1315"></a>
<span class="sourceLineNo">1316</span>                        while (it.hasNext()) {<a name="line.1316"></a>
<span class="sourceLineNo">1317</span>                                result.setObjectAbs(it.index, data.getInt(pos));<a name="line.1317"></a>
<span class="sourceLineNo">1318</span>                        }<a name="line.1318"></a>
<span class="sourceLineNo">1319</span>                        break;<a name="line.1319"></a>
<span class="sourceLineNo">1320</span>                case Dataset.INT64:<a name="line.1320"></a>
<span class="sourceLineNo">1321</span>                        while (it.hasNext()) {<a name="line.1321"></a>
<span class="sourceLineNo">1322</span>                                result.setObjectAbs(it.index, data.getLong(pos));<a name="line.1322"></a>
<span class="sourceLineNo">1323</span>                        }<a name="line.1323"></a>
<span class="sourceLineNo">1324</span>                        break;<a name="line.1324"></a>
<span class="sourceLineNo">1325</span>                case Dataset.FLOAT32:<a name="line.1325"></a>
<span class="sourceLineNo">1326</span>                        while (it.hasNext()) {<a name="line.1326"></a>
<span class="sourceLineNo">1327</span>                                result.setObjectAbs(it.index, data.getFloat(pos));<a name="line.1327"></a>
<span class="sourceLineNo">1328</span>                        }<a name="line.1328"></a>
<span class="sourceLineNo">1329</span>                        break;<a name="line.1329"></a>
<span class="sourceLineNo">1330</span>                case Dataset.FLOAT64:<a name="line.1330"></a>
<span class="sourceLineNo">1331</span>                        while (it.hasNext()) {<a name="line.1331"></a>
<span class="sourceLineNo">1332</span>                                result.setObjectAbs(it.index, data.getDouble(pos));<a name="line.1332"></a>
<span class="sourceLineNo">1333</span>                        }<a name="line.1333"></a>
<span class="sourceLineNo">1334</span>                        break;<a name="line.1334"></a>
<span class="sourceLineNo">1335</span>                default:<a name="line.1335"></a>
<span class="sourceLineNo">1336</span>                        while (it.hasNext()) {<a name="line.1336"></a>
<span class="sourceLineNo">1337</span>                                result.setObjectAbs(it.index, data.getObject(pos));<a name="line.1337"></a>
<span class="sourceLineNo">1338</span>                        }<a name="line.1338"></a>
<span class="sourceLineNo">1339</span>                        break;<a name="line.1339"></a>
<span class="sourceLineNo">1340</span>                }<a name="line.1340"></a>
<span class="sourceLineNo">1341</span><a name="line.1341"></a>
<span class="sourceLineNo">1342</span>                result.setErrors(data.getErrors());<a name="line.1342"></a>
<span class="sourceLineNo">1343</span>                return result;<a name="line.1343"></a>
<span class="sourceLineNo">1344</span>        }<a name="line.1344"></a>
<span class="sourceLineNo">1345</span><a name="line.1345"></a>
<span class="sourceLineNo">1346</span>        /**<a name="line.1346"></a>
<span class="sourceLineNo">1347</span>         * Create a compound dataset from given datasets<a name="line.1347"></a>
<span class="sourceLineNo">1348</span>         * @param datasets<a name="line.1348"></a>
<span class="sourceLineNo">1349</span>         * @return compound dataset or null if none given<a name="line.1349"></a>
<span class="sourceLineNo">1350</span>         */<a name="line.1350"></a>
<span class="sourceLineNo">1351</span>        public static CompoundDataset createCompoundDataset(final Dataset... datasets) {<a name="line.1351"></a>
<span class="sourceLineNo">1352</span>                if (datasets == null || datasets.length == 0)<a name="line.1352"></a>
<span class="sourceLineNo">1353</span>                        return null;<a name="line.1353"></a>
<span class="sourceLineNo">1354</span><a name="line.1354"></a>
<span class="sourceLineNo">1355</span>                return createCompoundDataset(datasets[0].getDType(), datasets);<a name="line.1355"></a>
<span class="sourceLineNo">1356</span>        }<a name="line.1356"></a>
<span class="sourceLineNo">1357</span><a name="line.1357"></a>
<span class="sourceLineNo">1358</span>        /**<a name="line.1358"></a>
<span class="sourceLineNo">1359</span>         * Create a compound dataset from given datasets<a name="line.1359"></a>
<span class="sourceLineNo">1360</span>         * @param dtype<a name="line.1360"></a>
<span class="sourceLineNo">1361</span>         * @param datasets<a name="line.1361"></a>
<span class="sourceLineNo">1362</span>         * @return compound dataset or null if none given<a name="line.1362"></a>
<span class="sourceLineNo">1363</span>         */<a name="line.1363"></a>
<span class="sourceLineNo">1364</span>        public static CompoundDataset createCompoundDataset(final int dtype, final Dataset... datasets) {<a name="line.1364"></a>
<span class="sourceLineNo">1365</span>                if (datasets == null || datasets.length == 0)<a name="line.1365"></a>
<span class="sourceLineNo">1366</span>                        return null;<a name="line.1366"></a>
<span class="sourceLineNo">1367</span><a name="line.1367"></a>
<span class="sourceLineNo">1368</span>                switch (dtype) {<a name="line.1368"></a>
<span class="sourceLineNo">1369</span>                case Dataset.INT8:<a name="line.1369"></a>
<span class="sourceLineNo">1370</span>                case Dataset.ARRAYINT8:<a name="line.1370"></a>
<span class="sourceLineNo">1371</span>                        return new CompoundByteDataset(datasets);<a name="line.1371"></a>
<span class="sourceLineNo">1372</span>                case Dataset.INT16:<a name="line.1372"></a>
<span class="sourceLineNo">1373</span>                case Dataset.ARRAYINT16:<a name="line.1373"></a>
<span class="sourceLineNo">1374</span>                        return new CompoundShortDataset(datasets);<a name="line.1374"></a>
<span class="sourceLineNo">1375</span>                case Dataset.INT32:<a name="line.1375"></a>
<span class="sourceLineNo">1376</span>                case Dataset.ARRAYINT32:<a name="line.1376"></a>
<span class="sourceLineNo">1377</span>                        return new CompoundIntegerDataset(datasets);<a name="line.1377"></a>
<span class="sourceLineNo">1378</span>                case Dataset.INT64:<a name="line.1378"></a>
<span class="sourceLineNo">1379</span>                case Dataset.ARRAYINT64:<a name="line.1379"></a>
<span class="sourceLineNo">1380</span>                        return new CompoundLongDataset(datasets);<a name="line.1380"></a>
<span class="sourceLineNo">1381</span>                case Dataset.FLOAT32:<a name="line.1381"></a>
<span class="sourceLineNo">1382</span>                case Dataset.ARRAYFLOAT32:<a name="line.1382"></a>
<span class="sourceLineNo">1383</span>                        return new CompoundFloatDataset(datasets);<a name="line.1383"></a>
<span class="sourceLineNo">1384</span>                case Dataset.FLOAT64:<a name="line.1384"></a>
<span class="sourceLineNo">1385</span>                case Dataset.ARRAYFLOAT64:<a name="line.1385"></a>
<span class="sourceLineNo">1386</span>                        return new CompoundDoubleDataset(datasets);<a name="line.1386"></a>
<span class="sourceLineNo">1387</span>                case Dataset.COMPLEX64:<a name="line.1387"></a>
<span class="sourceLineNo">1388</span>                case Dataset.COMPLEX128:<a name="line.1388"></a>
<span class="sourceLineNo">1389</span>                        if (datasets.length &gt; 2) {<a name="line.1389"></a>
<span class="sourceLineNo">1390</span>                                utilsLogger.error("At most two datasets are allowed");<a name="line.1390"></a>
<span class="sourceLineNo">1391</span>                                throw new IllegalArgumentException("At most two datasets are allowed");<a name="line.1391"></a>
<span class="sourceLineNo">1392</span>                        } else if (datasets.length == 2) {<a name="line.1392"></a>
<span class="sourceLineNo">1393</span>                                return dtype == Dataset.COMPLEX64 ? new ComplexFloatDataset(datasets[0], datasets[1]) : new ComplexDoubleDataset(datasets[0], datasets[1]);<a name="line.1393"></a>
<span class="sourceLineNo">1394</span>                        }<a name="line.1394"></a>
<span class="sourceLineNo">1395</span>                        return dtype == Dataset.COMPLEX64 ? new ComplexFloatDataset(datasets[0]) : new ComplexDoubleDataset(datasets[0]);<a name="line.1395"></a>
<span class="sourceLineNo">1396</span>                case Dataset.RGB:<a name="line.1396"></a>
<span class="sourceLineNo">1397</span>                        if (datasets.length == 1) {<a name="line.1397"></a>
<span class="sourceLineNo">1398</span>                                return new RGBDataset(datasets[0]);<a name="line.1398"></a>
<span class="sourceLineNo">1399</span>                        } else if (datasets.length == 3) {<a name="line.1399"></a>
<span class="sourceLineNo">1400</span>                                return new RGBDataset(datasets[0], datasets[1], datasets[2]);<a name="line.1400"></a>
<span class="sourceLineNo">1401</span>                        } else {<a name="line.1401"></a>
<span class="sourceLineNo">1402</span>                                utilsLogger.error("Only one or three datasets are allowed to create a RGB dataset");<a name="line.1402"></a>
<span class="sourceLineNo">1403</span>                                throw new IllegalArgumentException("Only one or three datasets are allowed to create a RGB dataset");<a name="line.1403"></a>
<span class="sourceLineNo">1404</span>                        }<a name="line.1404"></a>
<span class="sourceLineNo">1405</span>                default:<a name="line.1405"></a>
<span class="sourceLineNo">1406</span>                        utilsLogger.error("Dataset type not supported for this operation");<a name="line.1406"></a>
<span class="sourceLineNo">1407</span>                        throw new UnsupportedOperationException("Dataset type not supported");<a name="line.1407"></a>
<span class="sourceLineNo">1408</span>                }<a name="line.1408"></a>
<span class="sourceLineNo">1409</span>        }<a name="line.1409"></a>
<span class="sourceLineNo">1410</span><a name="line.1410"></a>
<span class="sourceLineNo">1411</span>        /**<a name="line.1411"></a>
<span class="sourceLineNo">1412</span>         * Create a compound dataset from given datasets<a name="line.1412"></a>
<span class="sourceLineNo">1413</span>         * @param clazz dataset class<a name="line.1413"></a>
<span class="sourceLineNo">1414</span>         * @param datasets<a name="line.1414"></a>
<span class="sourceLineNo">1415</span>         * @return compound dataset or null if none given<a name="line.1415"></a>
<span class="sourceLineNo">1416</span>         */<a name="line.1416"></a>
<span class="sourceLineNo">1417</span>        public static &lt;T extends CompoundDataset&gt; T createCompoundDataset(Class&lt;T&gt; clazz, final Dataset... datasets) {<a name="line.1417"></a>
<span class="sourceLineNo">1418</span>                return (T) createCompoundDataset(DTypeUtils.getDType(clazz), datasets);<a name="line.1418"></a>
<span class="sourceLineNo">1419</span>        }<a name="line.1419"></a>
<span class="sourceLineNo">1420</span><a name="line.1420"></a>
<span class="sourceLineNo">1421</span>        /**<a name="line.1421"></a>
<span class="sourceLineNo">1422</span>         * Create a compound dataset from given dataset<a name="line.1422"></a>
<span class="sourceLineNo">1423</span>         * @param dataset<a name="line.1423"></a>
<span class="sourceLineNo">1424</span>         * @param itemSize<a name="line.1424"></a>
<span class="sourceLineNo">1425</span>         * @return compound dataset<a name="line.1425"></a>
<span class="sourceLineNo">1426</span>         */<a name="line.1426"></a>
<span class="sourceLineNo">1427</span>        public static CompoundDataset createCompoundDataset(final Dataset dataset, final int itemSize) {<a name="line.1427"></a>
<span class="sourceLineNo">1428</span>                int[] shape = dataset.getShapeRef();<a name="line.1428"></a>
<span class="sourceLineNo">1429</span>                int[] nshape = shape;<a name="line.1429"></a>
<span class="sourceLineNo">1430</span>                if (shape != null &amp;&amp; itemSize &gt; 1) {<a name="line.1430"></a>
<span class="sourceLineNo">1431</span>                        int size = ShapeUtils.calcSize(shape);<a name="line.1431"></a>
<span class="sourceLineNo">1432</span>                        if (size % itemSize != 0) {<a name="line.1432"></a>
<span class="sourceLineNo">1433</span>                                throw new IllegalArgumentException("Input dataset has number of items that is not a multiple of itemSize");<a name="line.1433"></a>
<span class="sourceLineNo">1434</span>                        }<a name="line.1434"></a>
<span class="sourceLineNo">1435</span>                        int d = shape.length;<a name="line.1435"></a>
<span class="sourceLineNo">1436</span>                        int l = 1;<a name="line.1436"></a>
<span class="sourceLineNo">1437</span>                        while (--d &gt;= 0) {<a name="line.1437"></a>
<span class="sourceLineNo">1438</span>                                l *= shape[d];<a name="line.1438"></a>
<span class="sourceLineNo">1439</span>                                if (l % itemSize == 0) {<a name="line.1439"></a>
<span class="sourceLineNo">1440</span>                                        break;<a name="line.1440"></a>
<span class="sourceLineNo">1441</span>                                }<a name="line.1441"></a>
<span class="sourceLineNo">1442</span>                        }<a name="line.1442"></a>
<span class="sourceLineNo">1443</span>                        assert d &gt;= 0;<a name="line.1443"></a>
<span class="sourceLineNo">1444</span>                        nshape = new int[d + 1];<a name="line.1444"></a>
<span class="sourceLineNo">1445</span>                        for (int i = 0; i &lt; d; i++) {<a name="line.1445"></a>
<span class="sourceLineNo">1446</span>                                nshape[i] = shape[i];<a name="line.1446"></a>
<span class="sourceLineNo">1447</span>                        }<a name="line.1447"></a>
<span class="sourceLineNo">1448</span>                        nshape[d] = l / itemSize;<a name="line.1448"></a>
<span class="sourceLineNo">1449</span>                }<a name="line.1449"></a>
<span class="sourceLineNo">1450</span>                switch (dataset.getDType()) {<a name="line.1450"></a>
<span class="sourceLineNo">1451</span>                case Dataset.INT8:<a name="line.1451"></a>
<span class="sourceLineNo">1452</span>                        return new CompoundByteDataset(itemSize, (byte[]) dataset.getBuffer(), nshape);<a name="line.1452"></a>
<span class="sourceLineNo">1453</span>                case Dataset.INT16:<a name="line.1453"></a>
<span class="sourceLineNo">1454</span>                        return new CompoundShortDataset(itemSize, (short[]) dataset.getBuffer(), nshape);<a name="line.1454"></a>
<span class="sourceLineNo">1455</span>                case Dataset.INT32:<a name="line.1455"></a>
<span class="sourceLineNo">1456</span>                        return new CompoundIntegerDataset(itemSize, (int[]) dataset.getBuffer(), nshape);<a name="line.1456"></a>
<span class="sourceLineNo">1457</span>                case Dataset.INT64:<a name="line.1457"></a>
<span class="sourceLineNo">1458</span>                        return new CompoundLongDataset(itemSize, (long[]) dataset.getBuffer(), nshape);<a name="line.1458"></a>
<span class="sourceLineNo">1459</span>                case Dataset.FLOAT32:<a name="line.1459"></a>
<span class="sourceLineNo">1460</span>                        return new CompoundFloatDataset(itemSize, (float[]) dataset.getBuffer(), nshape);<a name="line.1460"></a>
<span class="sourceLineNo">1461</span>                case Dataset.FLOAT64:<a name="line.1461"></a>
<span class="sourceLineNo">1462</span>                        return new CompoundDoubleDataset(itemSize, (double[]) dataset.getBuffer(), nshape);<a name="line.1462"></a>
<span class="sourceLineNo">1463</span>                default:<a name="line.1463"></a>
<span class="sourceLineNo">1464</span>                        utilsLogger.error("Dataset type not supported for this operation");<a name="line.1464"></a>
<span class="sourceLineNo">1465</span>                        throw new UnsupportedOperationException("Dataset type not supported");<a name="line.1465"></a>
<span class="sourceLineNo">1466</span>                }<a name="line.1466"></a>
<span class="sourceLineNo">1467</span>        }<a name="line.1467"></a>
<span class="sourceLineNo">1468</span><a name="line.1468"></a>
<span class="sourceLineNo">1469</span><a name="line.1469"></a>
<span class="sourceLineNo">1470</span>        /**<a name="line.1470"></a>
<span class="sourceLineNo">1471</span>         * Create a compound dataset by using last axis as elements of an item<a name="line.1471"></a>
<span class="sourceLineNo">1472</span>         * @param a<a name="line.1472"></a>
<span class="sourceLineNo">1473</span>         * @param shareData if true, then share data<a name="line.1473"></a>
<span class="sourceLineNo">1474</span>         * @return compound dataset<a name="line.1474"></a>
<span class="sourceLineNo">1475</span>         */<a name="line.1475"></a>
<span class="sourceLineNo">1476</span>        public static CompoundDataset createCompoundDatasetFromLastAxis(final Dataset a, final boolean shareData) {<a name="line.1476"></a>
<span class="sourceLineNo">1477</span>                switch (a.getDType()) {<a name="line.1477"></a>
<span class="sourceLineNo">1478</span>                case Dataset.INT8:<a name="line.1478"></a>
<span class="sourceLineNo">1479</span>                        return CompoundByteDataset.createCompoundDatasetWithLastDimension(a, shareData);<a name="line.1479"></a>
<span class="sourceLineNo">1480</span>                case Dataset.INT16:<a name="line.1480"></a>
<span class="sourceLineNo">1481</span>                        return CompoundShortDataset.createCompoundDatasetWithLastDimension(a, shareData);<a name="line.1481"></a>
<span class="sourceLineNo">1482</span>                case Dataset.INT32:<a name="line.1482"></a>
<span class="sourceLineNo">1483</span>                        return CompoundIntegerDataset.createCompoundDatasetWithLastDimension(a, shareData);<a name="line.1483"></a>
<span class="sourceLineNo">1484</span>                case Dataset.INT64:<a name="line.1484"></a>
<span class="sourceLineNo">1485</span>                        return CompoundLongDataset.createCompoundDatasetWithLastDimension(a, shareData);<a name="line.1485"></a>
<span class="sourceLineNo">1486</span>                case Dataset.FLOAT32:<a name="line.1486"></a>
<span class="sourceLineNo">1487</span>                        return CompoundFloatDataset.createCompoundDatasetWithLastDimension(a, shareData);<a name="line.1487"></a>
<span class="sourceLineNo">1488</span>                case Dataset.FLOAT64:<a name="line.1488"></a>
<span class="sourceLineNo">1489</span>                        return CompoundDoubleDataset.createCompoundDatasetWithLastDimension(a, shareData);<a name="line.1489"></a>
<span class="sourceLineNo">1490</span>                default:<a name="line.1490"></a>
<span class="sourceLineNo">1491</span>                        utilsLogger.error("Dataset type not supported for this operation");<a name="line.1491"></a>
<span class="sourceLineNo">1492</span>                        throw new UnsupportedOperationException("Dataset type not supported");<a name="line.1492"></a>
<span class="sourceLineNo">1493</span>                }<a name="line.1493"></a>
<span class="sourceLineNo">1494</span>        }<a name="line.1494"></a>
<span class="sourceLineNo">1495</span><a name="line.1495"></a>
<span class="sourceLineNo">1496</span>        /**<a name="line.1496"></a>
<span class="sourceLineNo">1497</span>         * Create a dataset from a compound dataset by using elements of an item as last axis<a name="line.1497"></a>
<span class="sourceLineNo">1498</span>         * &lt;p&gt;<a name="line.1498"></a>
<span class="sourceLineNo">1499</span>         * In the case where the number of elements is one, the last axis is squeezed out.<a name="line.1499"></a>
<span class="sourceLineNo">1500</span>         * @param a<a name="line.1500"></a>
<span class="sourceLineNo">1501</span>         * @param shareData if true, then share data<a name="line.1501"></a>
<span class="sourceLineNo">1502</span>         * @return non-compound dataset<a name="line.1502"></a>
<span class="sourceLineNo">1503</span>         */<a name="line.1503"></a>
<span class="sourceLineNo">1504</span>        public static Dataset createDatasetFromCompoundDataset(final CompoundDataset a, final boolean shareData) {<a name="line.1504"></a>
<span class="sourceLineNo">1505</span>                return a.asNonCompoundDataset(shareData);<a name="line.1505"></a>
<span class="sourceLineNo">1506</span>        }<a name="line.1506"></a>
<span class="sourceLineNo">1507</span><a name="line.1507"></a>
<span class="sourceLineNo">1508</span>        /**<a name="line.1508"></a>
<span class="sourceLineNo">1509</span>         * Create a copy that has been coerced to an appropriate dataset type<a name="line.1509"></a>
<span class="sourceLineNo">1510</span>         * depending on the input object's class<a name="line.1510"></a>
<span class="sourceLineNo">1511</span>         *<a name="line.1511"></a>
<span class="sourceLineNo">1512</span>         * @param a<a name="line.1512"></a>
<span class="sourceLineNo">1513</span>         * @param obj<a name="line.1513"></a>
<span class="sourceLineNo">1514</span>         * @return coerced copy of dataset<a name="line.1514"></a>
<span class="sourceLineNo">1515</span>         */<a name="line.1515"></a>
<span class="sourceLineNo">1516</span>        public static Dataset coerce(Dataset a, Object obj) {<a name="line.1516"></a>
<span class="sourceLineNo">1517</span>                final int dt = a.getDType();<a name="line.1517"></a>
<span class="sourceLineNo">1518</span>                final int ot = DTypeUtils.getDTypeFromClass(obj.getClass());<a name="line.1518"></a>
<span class="sourceLineNo">1519</span><a name="line.1519"></a>
<span class="sourceLineNo">1520</span>                return cast(a.clone(), DTypeUtils.getBestDType(dt, ot));<a name="line.1520"></a>
<span class="sourceLineNo">1521</span>        }<a name="line.1521"></a>
<span class="sourceLineNo">1522</span><a name="line.1522"></a>
<span class="sourceLineNo">1523</span>        /**<a name="line.1523"></a>
<span class="sourceLineNo">1524</span>         * Function that returns a normalised dataset which is bounded between 0 and 1<a name="line.1524"></a>
<span class="sourceLineNo">1525</span>         * @param a dataset<a name="line.1525"></a>
<span class="sourceLineNo">1526</span>         * @return normalised dataset<a name="line.1526"></a>
<span class="sourceLineNo">1527</span>         */<a name="line.1527"></a>
<span class="sourceLineNo">1528</span>        public static Dataset norm(Dataset a) {<a name="line.1528"></a>
<span class="sourceLineNo">1529</span>                double amin = a.min().doubleValue();<a name="line.1529"></a>
<span class="sourceLineNo">1530</span>                double aptp = a.max().doubleValue() - amin;<a name="line.1530"></a>
<span class="sourceLineNo">1531</span>                Dataset temp = Maths.subtract(a, amin);<a name="line.1531"></a>
<span class="sourceLineNo">1532</span>                temp.idivide(aptp);<a name="line.1532"></a>
<span class="sourceLineNo">1533</span>                return temp;<a name="line.1533"></a>
<span class="sourceLineNo">1534</span>        }<a name="line.1534"></a>
<span class="sourceLineNo">1535</span><a name="line.1535"></a>
<span class="sourceLineNo">1536</span>        /**<a name="line.1536"></a>
<span class="sourceLineNo">1537</span>         * Function that returns a normalised compound dataset which is bounded between 0 and 1. There<a name="line.1537"></a>
<span class="sourceLineNo">1538</span>         * are (at least) two ways to normalise a compound dataset: per element - extrema for each element<a name="line.1538"></a>
<span class="sourceLineNo">1539</span>         * in a compound item is used, i.e. many min/max pairs; over all elements - extrema for all elements<a name="line.1539"></a>
<span class="sourceLineNo">1540</span>         * is used, i.e. one min/max pair.<a name="line.1540"></a>
<span class="sourceLineNo">1541</span>         * @param a dataset<a name="line.1541"></a>
<span class="sourceLineNo">1542</span>         * @param overAllElements if true, then normalise over all elements in each item<a name="line.1542"></a>
<span class="sourceLineNo">1543</span>         * @return normalised dataset<a name="line.1543"></a>
<span class="sourceLineNo">1544</span>         */<a name="line.1544"></a>
<span class="sourceLineNo">1545</span>        public static CompoundDataset norm(CompoundDataset a, boolean overAllElements) {<a name="line.1545"></a>
<span class="sourceLineNo">1546</span>                double[] amin = a.minItem();<a name="line.1546"></a>
<span class="sourceLineNo">1547</span>                double[] amax = a.maxItem();<a name="line.1547"></a>
<span class="sourceLineNo">1548</span>                final int is = a.getElementsPerItem();<a name="line.1548"></a>
<span class="sourceLineNo">1549</span>                Dataset result;<a name="line.1549"></a>
<span class="sourceLineNo">1550</span><a name="line.1550"></a>
<span class="sourceLineNo">1551</span>                if (overAllElements) {<a name="line.1551"></a>
<span class="sourceLineNo">1552</span>                        Arrays.sort(amin);<a name="line.1552"></a>
<span class="sourceLineNo">1553</span>                        Arrays.sort(amax);<a name="line.1553"></a>
<span class="sourceLineNo">1554</span>                        double aptp = amax[0] - amin[0];<a name="line.1554"></a>
<span class="sourceLineNo">1555</span><a name="line.1555"></a>
<span class="sourceLineNo">1556</span>                        result = Maths.subtract(a, amin[0]);<a name="line.1556"></a>
<span class="sourceLineNo">1557</span>                        result.idivide(aptp);<a name="line.1557"></a>
<span class="sourceLineNo">1558</span>                } else {<a name="line.1558"></a>
<span class="sourceLineNo">1559</span>                        double[] aptp = new double[is];<a name="line.1559"></a>
<span class="sourceLineNo">1560</span>                        for (int j = 0; j &lt; is; j++) {<a name="line.1560"></a>
<span class="sourceLineNo">1561</span>                                aptp[j] = amax[j] - amin[j];<a name="line.1561"></a>
<span class="sourceLineNo">1562</span>                        }<a name="line.1562"></a>
<span class="sourceLineNo">1563</span><a name="line.1563"></a>
<span class="sourceLineNo">1564</span>                        result = Maths.subtract(a, amin);<a name="line.1564"></a>
<span class="sourceLineNo">1565</span>                        result.idivide(aptp);<a name="line.1565"></a>
<span class="sourceLineNo">1566</span>                }<a name="line.1566"></a>
<span class="sourceLineNo">1567</span>                return (CompoundDataset) result;<a name="line.1567"></a>
<span class="sourceLineNo">1568</span>        }<a name="line.1568"></a>
<span class="sourceLineNo">1569</span><a name="line.1569"></a>
<span class="sourceLineNo">1570</span>        /**<a name="line.1570"></a>
<span class="sourceLineNo">1571</span>         * Function that returns a normalised dataset which is bounded between 0 and 1<a name="line.1571"></a>
<span class="sourceLineNo">1572</span>         * and has been distributed on a log10 scale<a name="line.1572"></a>
<span class="sourceLineNo">1573</span>         * @param a dataset<a name="line.1573"></a>
<span class="sourceLineNo">1574</span>         * @return normalised dataset<a name="line.1574"></a>
<span class="sourceLineNo">1575</span>         */<a name="line.1575"></a>
<span class="sourceLineNo">1576</span>        public static Dataset lognorm(Dataset a) {<a name="line.1576"></a>
<span class="sourceLineNo">1577</span>                double amin = a.min().doubleValue();<a name="line.1577"></a>
<span class="sourceLineNo">1578</span>                double aptp = Math.log10(a.max().doubleValue() - amin + 1.);<a name="line.1578"></a>
<span class="sourceLineNo">1579</span>                Dataset temp = Maths.subtract(a, amin - 1.);<a name="line.1579"></a>
<span class="sourceLineNo">1580</span>                temp = Maths.log10(temp);<a name="line.1580"></a>
<span class="sourceLineNo">1581</span>                temp = Maths.divide(temp, aptp);<a name="line.1581"></a>
<span class="sourceLineNo">1582</span>                return temp;<a name="line.1582"></a>
<span class="sourceLineNo">1583</span>        }<a name="line.1583"></a>
<span class="sourceLineNo">1584</span><a name="line.1584"></a>
<span class="sourceLineNo">1585</span>        /**<a name="line.1585"></a>
<span class="sourceLineNo">1586</span>         * Function that returns a normalised dataset which is bounded between 0 and 1<a name="line.1586"></a>
<span class="sourceLineNo">1587</span>         * and has been distributed on a natural log scale<a name="line.1587"></a>
<span class="sourceLineNo">1588</span>         * @param a dataset<a name="line.1588"></a>
<span class="sourceLineNo">1589</span>         * @return normalised dataset<a name="line.1589"></a>
<span class="sourceLineNo">1590</span>         */<a name="line.1590"></a>
<span class="sourceLineNo">1591</span>        public static Dataset lnnorm(Dataset a) {<a name="line.1591"></a>
<span class="sourceLineNo">1592</span>                double amin = a.min().doubleValue();<a name="line.1592"></a>
<span class="sourceLineNo">1593</span>                double aptp = Math.log(a.max().doubleValue() - amin + 1.);<a name="line.1593"></a>
<span class="sourceLineNo">1594</span>                Dataset temp = Maths.subtract(a, amin - 1.);<a name="line.1594"></a>
<span class="sourceLineNo">1595</span>                temp = Maths.log(temp);<a name="line.1595"></a>
<span class="sourceLineNo">1596</span>                temp = Maths.divide(temp, aptp);<a name="line.1596"></a>
<span class="sourceLineNo">1597</span>                return temp;<a name="line.1597"></a>
<span class="sourceLineNo">1598</span>        }<a name="line.1598"></a>
<span class="sourceLineNo">1599</span><a name="line.1599"></a>
<span class="sourceLineNo">1600</span>        /**<a name="line.1600"></a>
<span class="sourceLineNo">1601</span>         * Construct a list of datasets where each represents a coordinate varying over the hypergrid<a name="line.1601"></a>
<span class="sourceLineNo">1602</span>         * formed by the input list of axes<a name="line.1602"></a>
<span class="sourceLineNo">1603</span>         *<a name="line.1603"></a>
<span class="sourceLineNo">1604</span>         * @param axes an array of 1D datasets representing axes<a name="line.1604"></a>
<span class="sourceLineNo">1605</span>         * @return a list of coordinate datasets<a name="line.1605"></a>
<span class="sourceLineNo">1606</span>         */<a name="line.1606"></a>
<span class="sourceLineNo">1607</span>        public static List&lt;Dataset&gt; meshGrid(final Dataset... axes) {<a name="line.1607"></a>
<span class="sourceLineNo">1608</span>                List&lt;Dataset&gt; result = new ArrayList&lt;Dataset&gt;();<a name="line.1608"></a>
<span class="sourceLineNo">1609</span>                int rank = axes.length;<a name="line.1609"></a>
<span class="sourceLineNo">1610</span><a name="line.1610"></a>
<span class="sourceLineNo">1611</span>                if (rank &lt; 2) {<a name="line.1611"></a>
<span class="sourceLineNo">1612</span>                        utilsLogger.error("Two or more axes datasets are required");<a name="line.1612"></a>
<span class="sourceLineNo">1613</span>                        throw new IllegalArgumentException("Two or more axes datasets are required");<a name="line.1613"></a>
<span class="sourceLineNo">1614</span>                }<a name="line.1614"></a>
<span class="sourceLineNo">1615</span><a name="line.1615"></a>
<span class="sourceLineNo">1616</span>                int[] nshape = new int[rank];<a name="line.1616"></a>
<span class="sourceLineNo">1617</span><a name="line.1617"></a>
<span class="sourceLineNo">1618</span>                for (int i = 0; i &lt; rank; i++) {<a name="line.1618"></a>
<span class="sourceLineNo">1619</span>                        Dataset axis = axes[i];<a name="line.1619"></a>
<span class="sourceLineNo">1620</span>                        if (axis.getRank() != 1) {<a name="line.1620"></a>
<span class="sourceLineNo">1621</span>                                utilsLogger.error("Given axis is not 1D");<a name="line.1621"></a>
<span class="sourceLineNo">1622</span>                                throw new IllegalArgumentException("Given axis is not 1D");<a name="line.1622"></a>
<span class="sourceLineNo">1623</span>                        }<a name="line.1623"></a>
<span class="sourceLineNo">1624</span>                        nshape[i] = axis.getSize();<a name="line.1624"></a>
<span class="sourceLineNo">1625</span>                }<a name="line.1625"></a>
<span class="sourceLineNo">1626</span><a name="line.1626"></a>
<span class="sourceLineNo">1627</span>                for (int i = 0; i &lt; rank; i++) {<a name="line.1627"></a>
<span class="sourceLineNo">1628</span>                        Dataset axis = axes[i];<a name="line.1628"></a>
<span class="sourceLineNo">1629</span>                        @SuppressWarnings("deprecation")<a name="line.1629"></a>
<span class="sourceLineNo">1630</span>                        Dataset coord = DatasetFactory.zeros(nshape, axis.getDType());<a name="line.1630"></a>
<span class="sourceLineNo">1631</span>                        result.add(coord);<a name="line.1631"></a>
<span class="sourceLineNo">1632</span><a name="line.1632"></a>
<span class="sourceLineNo">1633</span>                        final int alen = axis.getSize();<a name="line.1633"></a>
<span class="sourceLineNo">1634</span>                        for (int j = 0; j &lt; alen; j++) {<a name="line.1634"></a>
<span class="sourceLineNo">1635</span>                                final Object obj = axis.getObjectAbs(j);<a name="line.1635"></a>
<span class="sourceLineNo">1636</span>                                PositionIterator pi = coord.getPositionIterator(i);<a name="line.1636"></a>
<span class="sourceLineNo">1637</span>                                final int[] pos = pi.getPos();<a name="line.1637"></a>
<span class="sourceLineNo">1638</span><a name="line.1638"></a>
<span class="sourceLineNo">1639</span>                                pos[i] = j;<a name="line.1639"></a>
<span class="sourceLineNo">1640</span>                                while (pi.hasNext()) {<a name="line.1640"></a>
<span class="sourceLineNo">1641</span>                                        coord.set(obj, pos);<a name="line.1641"></a>
<span class="sourceLineNo">1642</span>                                }<a name="line.1642"></a>
<span class="sourceLineNo">1643</span>                        }<a name="line.1643"></a>
<span class="sourceLineNo">1644</span>                }<a name="line.1644"></a>
<span class="sourceLineNo">1645</span><a name="line.1645"></a>
<span class="sourceLineNo">1646</span>                return result;<a name="line.1646"></a>
<span class="sourceLineNo">1647</span>        }<a name="line.1647"></a>
<span class="sourceLineNo">1648</span><a name="line.1648"></a>
<span class="sourceLineNo">1649</span>        /**<a name="line.1649"></a>
<span class="sourceLineNo">1650</span>         * Generate an index dataset for given dataset where sub-datasets contain index values<a name="line.1650"></a>
<span class="sourceLineNo">1651</span>         *<a name="line.1651"></a>
<span class="sourceLineNo">1652</span>         * @return an index dataset<a name="line.1652"></a>
<span class="sourceLineNo">1653</span>         */<a name="line.1653"></a>
<span class="sourceLineNo">1654</span>        public static IntegerDataset indices(int... shape) {<a name="line.1654"></a>
<span class="sourceLineNo">1655</span>                // now create another dataset to plot against<a name="line.1655"></a>
<span class="sourceLineNo">1656</span>                final int rank = shape.length;<a name="line.1656"></a>
<span class="sourceLineNo">1657</span>                int[] nshape = new int[rank+1];<a name="line.1657"></a>
<span class="sourceLineNo">1658</span>                nshape[0] = rank;<a name="line.1658"></a>
<span class="sourceLineNo">1659</span>                for (int i = 0; i &lt; rank; i++) {<a name="line.1659"></a>
<span class="sourceLineNo">1660</span>                        nshape[i+1] = shape[i];<a name="line.1660"></a>
<span class="sourceLineNo">1661</span>                }<a name="line.1661"></a>
<span class="sourceLineNo">1662</span><a name="line.1662"></a>
<span class="sourceLineNo">1663</span>                IntegerDataset index = new IntegerDataset(nshape);<a name="line.1663"></a>
<span class="sourceLineNo">1664</span><a name="line.1664"></a>
<span class="sourceLineNo">1665</span>                if (rank == 1) {<a name="line.1665"></a>
<span class="sourceLineNo">1666</span>                        final int alen = shape[0];<a name="line.1666"></a>
<span class="sourceLineNo">1667</span>                        int[] pos = new int[2];<a name="line.1667"></a>
<span class="sourceLineNo">1668</span>                        for (int j = 0; j &lt; alen; j++) {<a name="line.1668"></a>
<span class="sourceLineNo">1669</span>                                pos[1] = j;<a name="line.1669"></a>
<span class="sourceLineNo">1670</span>                                index.set(j, pos);<a name="line.1670"></a>
<span class="sourceLineNo">1671</span>                        }<a name="line.1671"></a>
<span class="sourceLineNo">1672</span>                } else {<a name="line.1672"></a>
<span class="sourceLineNo">1673</span>                        for (int i = 1; i &lt;= rank; i++) {<a name="line.1673"></a>
<span class="sourceLineNo">1674</span>                                final int alen = nshape[i];<a name="line.1674"></a>
<span class="sourceLineNo">1675</span>                                for (int j = 0; j &lt; alen; j++) {<a name="line.1675"></a>
<span class="sourceLineNo">1676</span>                                        PositionIterator pi = index.getPositionIterator(0, i);<a name="line.1676"></a>
<span class="sourceLineNo">1677</span>                                        final int[] pos = pi.getPos();<a name="line.1677"></a>
<span class="sourceLineNo">1678</span><a name="line.1678"></a>
<span class="sourceLineNo">1679</span>                                        pos[0] = i-1;<a name="line.1679"></a>
<span class="sourceLineNo">1680</span>                                        pos[i] = j;<a name="line.1680"></a>
<span class="sourceLineNo">1681</span>                                        while (pi.hasNext()) {<a name="line.1681"></a>
<span class="sourceLineNo">1682</span>                                                index.set(j, pos);<a name="line.1682"></a>
<span class="sourceLineNo">1683</span>                                        }<a name="line.1683"></a>
<span class="sourceLineNo">1684</span>                                }<a name="line.1684"></a>
<span class="sourceLineNo">1685</span>                        }<a name="line.1685"></a>
<span class="sourceLineNo">1686</span>                }<a name="line.1686"></a>
<span class="sourceLineNo">1687</span>                return index;<a name="line.1687"></a>
<span class="sourceLineNo">1688</span>        }<a name="line.1688"></a>
<span class="sourceLineNo">1689</span><a name="line.1689"></a>
<span class="sourceLineNo">1690</span>        /**<a name="line.1690"></a>
<span class="sourceLineNo">1691</span>         * Get the centroid value of a dataset, this function works out the centroid in every direction<a name="line.1691"></a>
<span class="sourceLineNo">1692</span>         *<a name="line.1692"></a>
<span class="sourceLineNo">1693</span>         * @param a<a name="line.1693"></a>
<span class="sourceLineNo">1694</span>         *            the dataset to be analysed<a name="line.1694"></a>
<span class="sourceLineNo">1695</span>         * @param bases the optional array of base coordinates to use as weights.<a name="line.1695"></a>
<span class="sourceLineNo">1696</span>         * This defaults to the mid-point of indices<a name="line.1696"></a>
<span class="sourceLineNo">1697</span>         * @return a double array containing the centroid for each dimension<a name="line.1697"></a>
<span class="sourceLineNo">1698</span>         */<a name="line.1698"></a>
<span class="sourceLineNo">1699</span>        public static double[] centroid(Dataset a, Dataset... bases) {<a name="line.1699"></a>
<span class="sourceLineNo">1700</span>                int rank = a.getRank();<a name="line.1700"></a>
<span class="sourceLineNo">1701</span>                if (bases.length &gt; 0 &amp;&amp; bases.length != rank) {<a name="line.1701"></a>
<span class="sourceLineNo">1702</span>                        throw new IllegalArgumentException("Number of bases must be zero or match rank of dataset");<a name="line.1702"></a>
<span class="sourceLineNo">1703</span>                }<a name="line.1703"></a>
<span class="sourceLineNo">1704</span><a name="line.1704"></a>
<span class="sourceLineNo">1705</span>                int[] shape = a.getShapeRef();<a name="line.1705"></a>
<span class="sourceLineNo">1706</span>                if (bases.length == rank) {<a name="line.1706"></a>
<span class="sourceLineNo">1707</span>                        for (int i = 0; i &lt; rank; i++) {<a name="line.1707"></a>
<span class="sourceLineNo">1708</span>                                Dataset b = bases[i];<a name="line.1708"></a>
<span class="sourceLineNo">1709</span>                                if (b.getRank() != 1 &amp;&amp; b.getSize() != shape[i]) {<a name="line.1709"></a>
<span class="sourceLineNo">1710</span>                                        throw new IllegalArgumentException("A base does not have shape to match given dataset");<a name="line.1710"></a>
<span class="sourceLineNo">1711</span>                                }<a name="line.1711"></a>
<span class="sourceLineNo">1712</span>                        }<a name="line.1712"></a>
<span class="sourceLineNo">1713</span>                }<a name="line.1713"></a>
<span class="sourceLineNo">1714</span><a name="line.1714"></a>
<span class="sourceLineNo">1715</span>                double[] dc = new double[rank];<a name="line.1715"></a>
<span class="sourceLineNo">1716</span>                if (rank == 0)<a name="line.1716"></a>
<span class="sourceLineNo">1717</span>                        return dc;<a name="line.1717"></a>
<span class="sourceLineNo">1718</span><a name="line.1718"></a>
<span class="sourceLineNo">1719</span>                final PositionIterator iter = new PositionIterator(shape);<a name="line.1719"></a>
<span class="sourceLineNo">1720</span>                final int[] pos = iter.getPos();<a name="line.1720"></a>
<span class="sourceLineNo">1721</span><a name="line.1721"></a>
<span class="sourceLineNo">1722</span>                double tsum = 0.0;<a name="line.1722"></a>
<span class="sourceLineNo">1723</span>                while (iter.hasNext()) {<a name="line.1723"></a>
<span class="sourceLineNo">1724</span>                        double val = a.getDouble(pos);<a name="line.1724"></a>
<span class="sourceLineNo">1725</span>                        tsum += val;<a name="line.1725"></a>
<span class="sourceLineNo">1726</span>                        for (int d = 0; d &lt; rank; d++) {<a name="line.1726"></a>
<span class="sourceLineNo">1727</span>                                Dataset b = bases.length == 0 ? null : bases[d];<a name="line.1727"></a>
<span class="sourceLineNo">1728</span>                                if (b == null) {<a name="line.1728"></a>
<span class="sourceLineNo">1729</span>                                        dc[d] += (pos[d] + 0.5) * val;<a name="line.1729"></a>
<span class="sourceLineNo">1730</span>                                } else {<a name="line.1730"></a>
<span class="sourceLineNo">1731</span>                                        dc[d] += b.getElementDoubleAbs(pos[d]) * val;<a name="line.1731"></a>
<span class="sourceLineNo">1732</span>                                }<a name="line.1732"></a>
<span class="sourceLineNo">1733</span>                        }<a name="line.1733"></a>
<span class="sourceLineNo">1734</span>                }<a name="line.1734"></a>
<span class="sourceLineNo">1735</span><a name="line.1735"></a>
<span class="sourceLineNo">1736</span>                for (int d = 0; d &lt; rank; d++) {<a name="line.1736"></a>
<span class="sourceLineNo">1737</span>                        dc[d] /= tsum;<a name="line.1737"></a>
<span class="sourceLineNo">1738</span>                }<a name="line.1738"></a>
<span class="sourceLineNo">1739</span>                return dc;<a name="line.1739"></a>
<span class="sourceLineNo">1740</span>        }<a name="line.1740"></a>
<span class="sourceLineNo">1741</span><a name="line.1741"></a>
<span class="sourceLineNo">1742</span>        /**<a name="line.1742"></a>
<span class="sourceLineNo">1743</span>         * Find linearly-interpolated crossing points where the given dataset crosses the given value<a name="line.1743"></a>
<span class="sourceLineNo">1744</span>         *<a name="line.1744"></a>
<span class="sourceLineNo">1745</span>         * @param d<a name="line.1745"></a>
<span class="sourceLineNo">1746</span>         * @param value<a name="line.1746"></a>
<span class="sourceLineNo">1747</span>         * @return list of interpolated indices<a name="line.1747"></a>
<span class="sourceLineNo">1748</span>         */<a name="line.1748"></a>
<span class="sourceLineNo">1749</span>        public static List&lt;Double&gt; crossings(Dataset d, double value) {<a name="line.1749"></a>
<span class="sourceLineNo">1750</span>                if (d.getRank() != 1) {<a name="line.1750"></a>
<span class="sourceLineNo">1751</span>                        utilsLogger.error("Only 1d datasets supported");<a name="line.1751"></a>
<span class="sourceLineNo">1752</span>                        throw new UnsupportedOperationException("Only 1d datasets supported");<a name="line.1752"></a>
<span class="sourceLineNo">1753</span>                }<a name="line.1753"></a>
<span class="sourceLineNo">1754</span>                List&lt;Double&gt; results = new ArrayList&lt;Double&gt;();<a name="line.1754"></a>
<span class="sourceLineNo">1755</span><a name="line.1755"></a>
<span class="sourceLineNo">1756</span>                // run through all pairs of points on the line and see if value lies within<a name="line.1756"></a>
<span class="sourceLineNo">1757</span>                IndexIterator it = d.getIterator();<a name="line.1757"></a>
<span class="sourceLineNo">1758</span>                double y1, y2;<a name="line.1758"></a>
<span class="sourceLineNo">1759</span><a name="line.1759"></a>
<span class="sourceLineNo">1760</span>                y2 = it.hasNext() ? d.getElementDoubleAbs(it.index) : 0;<a name="line.1760"></a>
<span class="sourceLineNo">1761</span>                double x = 1;<a name="line.1761"></a>
<span class="sourceLineNo">1762</span>                while (it.hasNext()) {<a name="line.1762"></a>
<span class="sourceLineNo">1763</span>                        y1 = y2;<a name="line.1763"></a>
<span class="sourceLineNo">1764</span>                        y2 = d.getElementDoubleAbs(it.index);<a name="line.1764"></a>
<span class="sourceLineNo">1765</span>                        // check if value lies within pair [y1, y2]<a name="line.1765"></a>
<span class="sourceLineNo">1766</span>                        if ((y1 &lt;= value &amp;&amp; value &lt; y2) || (y1 &gt; value &amp;&amp; y2 &lt;= value)) {<a name="line.1766"></a>
<span class="sourceLineNo">1767</span>                                final double f = (value - y2)/(y2 - y1); // negative distance from right to left<a name="line.1767"></a>
<span class="sourceLineNo">1768</span>                                results.add(x + f);<a name="line.1768"></a>
<span class="sourceLineNo">1769</span>                        }<a name="line.1769"></a>
<span class="sourceLineNo">1770</span>                        x++;<a name="line.1770"></a>
<span class="sourceLineNo">1771</span>                }<a name="line.1771"></a>
<span class="sourceLineNo">1772</span>                if (y2 == value) { // add end point of it intersects<a name="line.1772"></a>
<span class="sourceLineNo">1773</span>                        results.add(x);<a name="line.1773"></a>
<span class="sourceLineNo">1774</span>                }<a name="line.1774"></a>
<span class="sourceLineNo">1775</span><a name="line.1775"></a>
<span class="sourceLineNo">1776</span>                return results;<a name="line.1776"></a>
<span class="sourceLineNo">1777</span>        }<a name="line.1777"></a>
<span class="sourceLineNo">1778</span><a name="line.1778"></a>
<span class="sourceLineNo">1779</span>        /**<a name="line.1779"></a>
<span class="sourceLineNo">1780</span>         * Find x values of all the crossing points of the dataset with the given y value<a name="line.1780"></a>
<span class="sourceLineNo">1781</span>         *<a name="line.1781"></a>
<span class="sourceLineNo">1782</span>         * @param xAxis<a name="line.1782"></a>
<span class="sourceLineNo">1783</span>         *            Dataset of the X axis that needs to be looked at<a name="line.1783"></a>
<span class="sourceLineNo">1784</span>         * @param yAxis<a name="line.1784"></a>
<span class="sourceLineNo">1785</span>         *            Dataset of the Y axis that needs to be looked at<a name="line.1785"></a>
<span class="sourceLineNo">1786</span>         * @param yValue<a name="line.1786"></a>
<span class="sourceLineNo">1787</span>         *            The y value the X values are required for<a name="line.1787"></a>
<span class="sourceLineNo">1788</span>         * @return An list of doubles containing all the X coordinates of where the line crosses<a name="line.1788"></a>
<span class="sourceLineNo">1789</span>         */<a name="line.1789"></a>
<span class="sourceLineNo">1790</span>        public static List&lt;Double&gt; crossings(Dataset xAxis, Dataset yAxis, double yValue) {<a name="line.1790"></a>
<span class="sourceLineNo">1791</span>                if (xAxis.getSize() &gt; yAxis.getSize()) {<a name="line.1791"></a>
<span class="sourceLineNo">1792</span>                        throw new IllegalArgumentException(<a name="line.1792"></a>
<span class="sourceLineNo">1793</span>                                        "Number of values of yAxis must as least be equal to the number of values of xAxis");<a name="line.1793"></a>
<span class="sourceLineNo">1794</span>                }<a name="line.1794"></a>
<span class="sourceLineNo">1795</span><a name="line.1795"></a>
<span class="sourceLineNo">1796</span>                List&lt;Double&gt; results = new ArrayList&lt;Double&gt;();<a name="line.1796"></a>
<span class="sourceLineNo">1797</span><a name="line.1797"></a>
<span class="sourceLineNo">1798</span>                List&lt;Double&gt; indices = crossings(yAxis, yValue);<a name="line.1798"></a>
<span class="sourceLineNo">1799</span><a name="line.1799"></a>
<span class="sourceLineNo">1800</span>                for (double xi : indices) {<a name="line.1800"></a>
<span class="sourceLineNo">1801</span>                        results.add(Maths.interpolate(xAxis, xi));<a name="line.1801"></a>
<span class="sourceLineNo">1802</span>                }<a name="line.1802"></a>
<span class="sourceLineNo">1803</span>                return results;<a name="line.1803"></a>
<span class="sourceLineNo">1804</span>        }<a name="line.1804"></a>
<span class="sourceLineNo">1805</span><a name="line.1805"></a>
<span class="sourceLineNo">1806</span>        /**<a name="line.1806"></a>
<span class="sourceLineNo">1807</span>         * Function that uses the crossings function but prunes the result, so that multiple crossings within a<a name="line.1807"></a>
<span class="sourceLineNo">1808</span>         * certain proportion of the overall range of the x values<a name="line.1808"></a>
<span class="sourceLineNo">1809</span>         *<a name="line.1809"></a>
<span class="sourceLineNo">1810</span>         * @param xAxis<a name="line.1810"></a>
<span class="sourceLineNo">1811</span>         *            Dataset of the X axis<a name="line.1811"></a>
<span class="sourceLineNo">1812</span>         * @param yAxis<a name="line.1812"></a>
<span class="sourceLineNo">1813</span>         *            Dataset of the Y axis<a name="line.1813"></a>
<span class="sourceLineNo">1814</span>         * @param yValue<a name="line.1814"></a>
<span class="sourceLineNo">1815</span>         *            The y value the x values are required for<a name="line.1815"></a>
<span class="sourceLineNo">1816</span>         * @param xRangeProportion<a name="line.1816"></a>
<span class="sourceLineNo">1817</span>         *            The proportion of the overall x spread used to prune result<a name="line.1817"></a>
<span class="sourceLineNo">1818</span>         * @return A list containing all the unique crossing points<a name="line.1818"></a>
<span class="sourceLineNo">1819</span>         */<a name="line.1819"></a>
<span class="sourceLineNo">1820</span>        public static List&lt;Double&gt; crossings(Dataset xAxis, Dataset yAxis, double yValue, double xRangeProportion) {<a name="line.1820"></a>
<span class="sourceLineNo">1821</span>                // get the values found<a name="line.1821"></a>
<span class="sourceLineNo">1822</span>                List&lt;Double&gt; vals = crossings(xAxis, yAxis, yValue);<a name="line.1822"></a>
<span class="sourceLineNo">1823</span><a name="line.1823"></a>
<span class="sourceLineNo">1824</span>                // use the proportion to calculate the error spacing<a name="line.1824"></a>
<span class="sourceLineNo">1825</span>                double error = xRangeProportion * xAxis.peakToPeak().doubleValue();<a name="line.1825"></a>
<span class="sourceLineNo">1826</span><a name="line.1826"></a>
<span class="sourceLineNo">1827</span>                int i = 0;<a name="line.1827"></a>
<span class="sourceLineNo">1828</span>                // now go through and check for groups of three crossings which are all<a name="line.1828"></a>
<span class="sourceLineNo">1829</span>                // within the boundaries<a name="line.1829"></a>
<span class="sourceLineNo">1830</span>                while (i &lt;= vals.size() - 3) {<a name="line.1830"></a>
<span class="sourceLineNo">1831</span>                        double v1 = Math.abs(vals.get(i) - vals.get(i + 2));<a name="line.1831"></a>
<span class="sourceLineNo">1832</span>                        if (v1 &lt; error) {<a name="line.1832"></a>
<span class="sourceLineNo">1833</span>                                // these 3 points should be treated as one<a name="line.1833"></a>
<span class="sourceLineNo">1834</span>                                // make the first point equal to the average of them all<a name="line.1834"></a>
<span class="sourceLineNo">1835</span>                                vals.set(i + 2, ((vals.get(i) + vals.get(i + 1) + vals.get(i + 2)) / 3.0));<a name="line.1835"></a>
<span class="sourceLineNo">1836</span>                                // remove the other offending points<a name="line.1836"></a>
<span class="sourceLineNo">1837</span>                                vals.remove(i);<a name="line.1837"></a>
<span class="sourceLineNo">1838</span>                                vals.remove(i);<a name="line.1838"></a>
<span class="sourceLineNo">1839</span>                        } else {<a name="line.1839"></a>
<span class="sourceLineNo">1840</span>                                i++;<a name="line.1840"></a>
<span class="sourceLineNo">1841</span>                        }<a name="line.1841"></a>
<span class="sourceLineNo">1842</span>                }<a name="line.1842"></a>
<span class="sourceLineNo">1843</span><a name="line.1843"></a>
<span class="sourceLineNo">1844</span>                // once the thinning process has been completed, return the pruned list<a name="line.1844"></a>
<span class="sourceLineNo">1845</span>                return vals;<a name="line.1845"></a>
<span class="sourceLineNo">1846</span>        }<a name="line.1846"></a>
<span class="sourceLineNo">1847</span><a name="line.1847"></a>
<span class="sourceLineNo">1848</span>        // recursive function<a name="line.1848"></a>
<span class="sourceLineNo">1849</span>        private static void setRow(Object row, Dataset a, int... pos) {<a name="line.1849"></a>
<span class="sourceLineNo">1850</span>                final int l = Array.getLength(row);<a name="line.1850"></a>
<span class="sourceLineNo">1851</span>                final int rank = pos.length;<a name="line.1851"></a>
<span class="sourceLineNo">1852</span>                final int[] npos = Arrays.copyOf(pos, rank+1);<a name="line.1852"></a>
<span class="sourceLineNo">1853</span>                Object r;<a name="line.1853"></a>
<span class="sourceLineNo">1854</span>                if (rank+1 &lt; a.getRank()) {<a name="line.1854"></a>
<span class="sourceLineNo">1855</span>                        for (int i = 0; i &lt; l; i++) {<a name="line.1855"></a>
<span class="sourceLineNo">1856</span>                                npos[rank] = i;<a name="line.1856"></a>
<span class="sourceLineNo">1857</span>                                r = Array.get(row, i);<a name="line.1857"></a>
<span class="sourceLineNo">1858</span>                                setRow(r, a, npos);<a name="line.1858"></a>
<span class="sourceLineNo">1859</span>                        }<a name="line.1859"></a>
<span class="sourceLineNo">1860</span>                } else {<a name="line.1860"></a>
<span class="sourceLineNo">1861</span>                        for (int i = 0; i &lt; l; i++) {<a name="line.1861"></a>
<span class="sourceLineNo">1862</span>                                npos[rank] = i;<a name="line.1862"></a>
<span class="sourceLineNo">1863</span>                                r = a.getObject(npos);<a name="line.1863"></a>
<span class="sourceLineNo">1864</span>                                Array.set(row, i, r);<a name="line.1864"></a>
<span class="sourceLineNo">1865</span>                        }<a name="line.1865"></a>
<span class="sourceLineNo">1866</span>                }<a name="line.1866"></a>
<span class="sourceLineNo">1867</span>        }<a name="line.1867"></a>
<span class="sourceLineNo">1868</span><a name="line.1868"></a>
<span class="sourceLineNo">1869</span>        /**<a name="line.1869"></a>
<span class="sourceLineNo">1870</span>         * Create Java array (of arrays) from dataset<a name="line.1870"></a>
<span class="sourceLineNo">1871</span>         * @param a dataset<a name="line.1871"></a>
<span class="sourceLineNo">1872</span>         * @return Java array (of arrays...)<a name="line.1872"></a>
<span class="sourceLineNo">1873</span>         */<a name="line.1873"></a>
<span class="sourceLineNo">1874</span>        public static Object createJavaArray(Dataset a) {<a name="line.1874"></a>
<span class="sourceLineNo">1875</span>                if (a.getElementsPerItem() &gt; 1) {<a name="line.1875"></a>
<span class="sourceLineNo">1876</span>                        a = createDatasetFromCompoundDataset((CompoundDataset) a, true);<a name="line.1876"></a>
<span class="sourceLineNo">1877</span>                }<a name="line.1877"></a>
<span class="sourceLineNo">1878</span>                Object matrix;<a name="line.1878"></a>
<span class="sourceLineNo">1879</span><a name="line.1879"></a>
<span class="sourceLineNo">1880</span>                switch (a.getDType()) {<a name="line.1880"></a>
<span class="sourceLineNo">1881</span>                case Dataset.BOOL:<a name="line.1881"></a>
<span class="sourceLineNo">1882</span>                        matrix = Array.newInstance(boolean.class, a.getShape());<a name="line.1882"></a>
<span class="sourceLineNo">1883</span>                        break;<a name="line.1883"></a>
<span class="sourceLineNo">1884</span>                case Dataset.INT8:<a name="line.1884"></a>
<span class="sourceLineNo">1885</span>                        matrix = Array.newInstance(byte.class, a.getShape());<a name="line.1885"></a>
<span class="sourceLineNo">1886</span>                        break;<a name="line.1886"></a>
<span class="sourceLineNo">1887</span>                case Dataset.INT16:<a name="line.1887"></a>
<span class="sourceLineNo">1888</span>                        matrix = Array.newInstance(short.class, a.getShape());<a name="line.1888"></a>
<span class="sourceLineNo">1889</span>                        break;<a name="line.1889"></a>
<span class="sourceLineNo">1890</span>                case Dataset.INT32:<a name="line.1890"></a>
<span class="sourceLineNo">1891</span>                        matrix = Array.newInstance(int.class, a.getShape());<a name="line.1891"></a>
<span class="sourceLineNo">1892</span>                        break;<a name="line.1892"></a>
<span class="sourceLineNo">1893</span>                case Dataset.INT64:<a name="line.1893"></a>
<span class="sourceLineNo">1894</span>                        matrix = Array.newInstance(long.class, a.getShape());<a name="line.1894"></a>
<span class="sourceLineNo">1895</span>                        break;<a name="line.1895"></a>
<span class="sourceLineNo">1896</span>                case Dataset.FLOAT32:<a name="line.1896"></a>
<span class="sourceLineNo">1897</span>                        matrix = Array.newInstance(float.class, a.getShape());<a name="line.1897"></a>
<span class="sourceLineNo">1898</span>                        break;<a name="line.1898"></a>
<span class="sourceLineNo">1899</span>                case Dataset.FLOAT64:<a name="line.1899"></a>
<span class="sourceLineNo">1900</span>                        matrix = Array.newInstance(double.class, a.getShape());<a name="line.1900"></a>
<span class="sourceLineNo">1901</span>                        break;<a name="line.1901"></a>
<span class="sourceLineNo">1902</span>                default:<a name="line.1902"></a>
<span class="sourceLineNo">1903</span>                        utilsLogger.error("Dataset type not supported");<a name="line.1903"></a>
<span class="sourceLineNo">1904</span>                        throw new IllegalArgumentException("Dataset type not supported");<a name="line.1904"></a>
<span class="sourceLineNo">1905</span>                }<a name="line.1905"></a>
<span class="sourceLineNo">1906</span><a name="line.1906"></a>
<span class="sourceLineNo">1907</span>                // populate matrix<a name="line.1907"></a>
<span class="sourceLineNo">1908</span>                setRow(matrix, a);<a name="line.1908"></a>
<span class="sourceLineNo">1909</span>                return matrix;<a name="line.1909"></a>
<span class="sourceLineNo">1910</span>        }<a name="line.1910"></a>
<span class="sourceLineNo">1911</span><a name="line.1911"></a>
<span class="sourceLineNo">1912</span>        /**<a name="line.1912"></a>
<span class="sourceLineNo">1913</span>         * Removes NaNs and infinities from floating point datasets.<a name="line.1913"></a>
<span class="sourceLineNo">1914</span>         * All other dataset types are ignored.<a name="line.1914"></a>
<span class="sourceLineNo">1915</span>         *<a name="line.1915"></a>
<span class="sourceLineNo">1916</span>         * @param a dataset<a name="line.1916"></a>
<span class="sourceLineNo">1917</span>         * @param value replacement value<a name="line.1917"></a>
<span class="sourceLineNo">1918</span>         */<a name="line.1918"></a>
<span class="sourceLineNo">1919</span>        public static void removeNansAndInfinities(Dataset a, final Number value) {<a name="line.1919"></a>
<span class="sourceLineNo">1920</span>                if (a instanceof DoubleDataset) {<a name="line.1920"></a>
<span class="sourceLineNo">1921</span>                        final double dvalue = DTypeUtils.toReal(value);<a name="line.1921"></a>
<span class="sourceLineNo">1922</span>                        final DoubleDataset set = (DoubleDataset) a;<a name="line.1922"></a>
<span class="sourceLineNo">1923</span>                        final IndexIterator it = set.getIterator();<a name="line.1923"></a>
<span class="sourceLineNo">1924</span>                        final double[] data = set.getData();<a name="line.1924"></a>
<span class="sourceLineNo">1925</span>                        while (it.hasNext()) {<a name="line.1925"></a>
<span class="sourceLineNo">1926</span>                                double x = data[it.index];<a name="line.1926"></a>
<span class="sourceLineNo">1927</span>                                if (Double.isNaN(x) || Double.isInfinite(x))<a name="line.1927"></a>
<span class="sourceLineNo">1928</span>                                        data[it.index] = dvalue;<a name="line.1928"></a>
<span class="sourceLineNo">1929</span>                        }<a name="line.1929"></a>
<span class="sourceLineNo">1930</span>                } else if (a instanceof FloatDataset) {<a name="line.1930"></a>
<span class="sourceLineNo">1931</span>                        final float fvalue = (float) DTypeUtils.toReal(value);<a name="line.1931"></a>
<span class="sourceLineNo">1932</span>                        final FloatDataset set = (FloatDataset) a;<a name="line.1932"></a>
<span class="sourceLineNo">1933</span>                        final IndexIterator it = set.getIterator();<a name="line.1933"></a>
<span class="sourceLineNo">1934</span>                        final float[] data = set.getData();<a name="line.1934"></a>
<span class="sourceLineNo">1935</span>                        while (it.hasNext()) {<a name="line.1935"></a>
<span class="sourceLineNo">1936</span>                                float x = data[it.index];<a name="line.1936"></a>
<span class="sourceLineNo">1937</span>                                if (Float.isNaN(x) || Float.isInfinite(x))<a name="line.1937"></a>
<span class="sourceLineNo">1938</span>                                        data[it.index] = fvalue;<a name="line.1938"></a>
<span class="sourceLineNo">1939</span>                        }<a name="line.1939"></a>
<span class="sourceLineNo">1940</span>                } else if (a instanceof CompoundDoubleDataset) {<a name="line.1940"></a>
<span class="sourceLineNo">1941</span>                        final double dvalue = DTypeUtils.toReal(value);<a name="line.1941"></a>
<span class="sourceLineNo">1942</span>                        final CompoundDoubleDataset set = (CompoundDoubleDataset) a;<a name="line.1942"></a>
<span class="sourceLineNo">1943</span>                        final int is = set.getElementsPerItem();<a name="line.1943"></a>
<span class="sourceLineNo">1944</span>                        final IndexIterator it = set.getIterator();<a name="line.1944"></a>
<span class="sourceLineNo">1945</span>                        final double[] data = set.getData();<a name="line.1945"></a>
<span class="sourceLineNo">1946</span>                        while (it.hasNext()) {<a name="line.1946"></a>
<span class="sourceLineNo">1947</span>                                for (int j = 0; j &lt; is; j++) {<a name="line.1947"></a>
<span class="sourceLineNo">1948</span>                                        double x = data[it.index + j];<a name="line.1948"></a>
<span class="sourceLineNo">1949</span>                                        if (Double.isNaN(x) || Double.isInfinite(x))<a name="line.1949"></a>
<span class="sourceLineNo">1950</span>                                                data[it.index + j] = dvalue;<a name="line.1950"></a>
<span class="sourceLineNo">1951</span>                                }<a name="line.1951"></a>
<span class="sourceLineNo">1952</span>                        }<a name="line.1952"></a>
<span class="sourceLineNo">1953</span>                } else if (a instanceof CompoundFloatDataset) {<a name="line.1953"></a>
<span class="sourceLineNo">1954</span>                        final float fvalue = (float) DTypeUtils.toReal(value);<a name="line.1954"></a>
<span class="sourceLineNo">1955</span>                        final CompoundFloatDataset set = (CompoundFloatDataset) a;<a name="line.1955"></a>
<span class="sourceLineNo">1956</span>                        final int is = set.getElementsPerItem();<a name="line.1956"></a>
<span class="sourceLineNo">1957</span>                        final IndexIterator it = set.getIterator();<a name="line.1957"></a>
<span class="sourceLineNo">1958</span>                        final float[] data = set.getData();<a name="line.1958"></a>
<span class="sourceLineNo">1959</span>                        while (it.hasNext()) {<a name="line.1959"></a>
<span class="sourceLineNo">1960</span>                                for (int j = 0; j &lt; is; j++) {<a name="line.1960"></a>
<span class="sourceLineNo">1961</span>                                        float x = data[it.index + j];<a name="line.1961"></a>
<span class="sourceLineNo">1962</span>                                        if (Float.isNaN(x) || Float.isInfinite(x))<a name="line.1962"></a>
<span class="sourceLineNo">1963</span>                                                data[it.index + j] = fvalue;<a name="line.1963"></a>
<span class="sourceLineNo">1964</span>                                }<a name="line.1964"></a>
<span class="sourceLineNo">1965</span>                        }<a name="line.1965"></a>
<span class="sourceLineNo">1966</span>                }<a name="line.1966"></a>
<span class="sourceLineNo">1967</span>        }<a name="line.1967"></a>
<span class="sourceLineNo">1968</span><a name="line.1968"></a>
<span class="sourceLineNo">1969</span>        /**<a name="line.1969"></a>
<span class="sourceLineNo">1970</span>         * Make floating point datasets contain only finite values. Infinities and NaNs are replaced<a name="line.1970"></a>
<span class="sourceLineNo">1971</span>         * by +/- MAX_VALUE and 0, respectively.<a name="line.1971"></a>
<span class="sourceLineNo">1972</span>         * All other dataset types are ignored.<a name="line.1972"></a>
<span class="sourceLineNo">1973</span>         *<a name="line.1973"></a>
<span class="sourceLineNo">1974</span>         * @param a dataset<a name="line.1974"></a>
<span class="sourceLineNo">1975</span>         */<a name="line.1975"></a>
<span class="sourceLineNo">1976</span>        public static void makeFinite(Dataset a) {<a name="line.1976"></a>
<span class="sourceLineNo">1977</span>                if (a instanceof DoubleDataset) {<a name="line.1977"></a>
<span class="sourceLineNo">1978</span>                        final DoubleDataset set = (DoubleDataset) a;<a name="line.1978"></a>
<span class="sourceLineNo">1979</span>                        final IndexIterator it = set.getIterator();<a name="line.1979"></a>
<span class="sourceLineNo">1980</span>                        final double[] data = set.getData();<a name="line.1980"></a>
<span class="sourceLineNo">1981</span>                        while (it.hasNext()) {<a name="line.1981"></a>
<span class="sourceLineNo">1982</span>                                final double x = data[it.index];<a name="line.1982"></a>
<span class="sourceLineNo">1983</span>                                if (Double.isNaN(x))<a name="line.1983"></a>
<span class="sourceLineNo">1984</span>                                        data[it.index] = 0;<a name="line.1984"></a>
<span class="sourceLineNo">1985</span>                                else if (Double.isInfinite(x))<a name="line.1985"></a>
<span class="sourceLineNo">1986</span>                                        data[it.index] = x &gt; 0 ? Double.MAX_VALUE : -Double.MAX_VALUE;<a name="line.1986"></a>
<span class="sourceLineNo">1987</span>                        }<a name="line.1987"></a>
<span class="sourceLineNo">1988</span>                } else if (a instanceof FloatDataset) {<a name="line.1988"></a>
<span class="sourceLineNo">1989</span>                        final FloatDataset set = (FloatDataset) a;<a name="line.1989"></a>
<span class="sourceLineNo">1990</span>                        final IndexIterator it = set.getIterator();<a name="line.1990"></a>
<span class="sourceLineNo">1991</span>                        final float[] data = set.getData();<a name="line.1991"></a>
<span class="sourceLineNo">1992</span>                        while (it.hasNext()) {<a name="line.1992"></a>
<span class="sourceLineNo">1993</span>                                final float x = data[it.index];<a name="line.1993"></a>
<span class="sourceLineNo">1994</span>                                if (Float.isNaN(x))<a name="line.1994"></a>
<span class="sourceLineNo">1995</span>                                        data[it.index] = 0;<a name="line.1995"></a>
<span class="sourceLineNo">1996</span>                                else if (Float.isInfinite(x))<a name="line.1996"></a>
<span class="sourceLineNo">1997</span>                                        data[it.index] = x &gt; 0 ? Float.MAX_VALUE : -Float.MAX_VALUE;<a name="line.1997"></a>
<span class="sourceLineNo">1998</span>                        }<a name="line.1998"></a>
<span class="sourceLineNo">1999</span>                } else if (a instanceof CompoundDoubleDataset) {<a name="line.1999"></a>
<span class="sourceLineNo">2000</span>                        final CompoundDoubleDataset set = (CompoundDoubleDataset) a;<a name="line.2000"></a>
<span class="sourceLineNo">2001</span>                        final int is = set.getElementsPerItem();<a name="line.2001"></a>
<span class="sourceLineNo">2002</span>                        final IndexIterator it = set.getIterator();<a name="line.2002"></a>
<span class="sourceLineNo">2003</span>                        final double[] data = set.getData();<a name="line.2003"></a>
<span class="sourceLineNo">2004</span>                        while (it.hasNext()) {<a name="line.2004"></a>
<span class="sourceLineNo">2005</span>                                for (int j = 0; j &lt; is; j++) {<a name="line.2005"></a>
<span class="sourceLineNo">2006</span>                                        final double x = data[it.index + j];<a name="line.2006"></a>
<span class="sourceLineNo">2007</span>                                        if (Double.isNaN(x))<a name="line.2007"></a>
<span class="sourceLineNo">2008</span>                                                data[it.index + j] = 0;<a name="line.2008"></a>
<span class="sourceLineNo">2009</span>                                        else if (Double.isInfinite(x))<a name="line.2009"></a>
<span class="sourceLineNo">2010</span>                                                data[it.index + j] = x &gt; 0 ? Double.MAX_VALUE : -Double.MAX_VALUE;<a name="line.2010"></a>
<span class="sourceLineNo">2011</span>                                }<a name="line.2011"></a>
<span class="sourceLineNo">2012</span>                        }<a name="line.2012"></a>
<span class="sourceLineNo">2013</span>                } else if (a instanceof CompoundFloatDataset) {<a name="line.2013"></a>
<span class="sourceLineNo">2014</span>                        final CompoundFloatDataset set = (CompoundFloatDataset) a;<a name="line.2014"></a>
<span class="sourceLineNo">2015</span>                        final int is = set.getElementsPerItem();<a name="line.2015"></a>
<span class="sourceLineNo">2016</span>                        final IndexIterator it = set.getIterator();<a name="line.2016"></a>
<span class="sourceLineNo">2017</span>                        final float[] data = set.getData();<a name="line.2017"></a>
<span class="sourceLineNo">2018</span>                        while (it.hasNext()) {<a name="line.2018"></a>
<span class="sourceLineNo">2019</span>                                for (int j = 0; j &lt; is; j++) {<a name="line.2019"></a>
<span class="sourceLineNo">2020</span>                                        final float x = data[it.index + j];<a name="line.2020"></a>
<span class="sourceLineNo">2021</span>                                        if (Float.isNaN(x))<a name="line.2021"></a>
<span class="sourceLineNo">2022</span>                                                data[it.index + j] = 0;<a name="line.2022"></a>
<span class="sourceLineNo">2023</span>                                        else if (Float.isInfinite(x))<a name="line.2023"></a>
<span class="sourceLineNo">2024</span>                                                data[it.index + j] = x &gt; 0 ? Float.MAX_VALUE : -Float.MAX_VALUE;<a name="line.2024"></a>
<span class="sourceLineNo">2025</span>                                }<a name="line.2025"></a>
<span class="sourceLineNo">2026</span>                        }<a name="line.2026"></a>
<span class="sourceLineNo">2027</span>                }<a name="line.2027"></a>
<span class="sourceLineNo">2028</span>        }<a name="line.2028"></a>
<span class="sourceLineNo">2029</span><a name="line.2029"></a>
<span class="sourceLineNo">2030</span>        /**<a name="line.2030"></a>
<span class="sourceLineNo">2031</span>         * Find absolute index of first value in dataset that is equal to given number<a name="line.2031"></a>
<span class="sourceLineNo">2032</span>         * @param a<a name="line.2032"></a>
<span class="sourceLineNo">2033</span>         * @param n<a name="line.2033"></a>
<span class="sourceLineNo">2034</span>         * @return absolute index (if greater than a.getSize() then no value found)<a name="line.2034"></a>
<span class="sourceLineNo">2035</span>         */<a name="line.2035"></a>
<span class="sourceLineNo">2036</span>        public static int findIndexEqualTo(final Dataset a, final double n) {<a name="line.2036"></a>
<span class="sourceLineNo">2037</span>                IndexIterator iter = a.getIterator();<a name="line.2037"></a>
<span class="sourceLineNo">2038</span>                while (iter.hasNext()) {<a name="line.2038"></a>
<span class="sourceLineNo">2039</span>                        if (a.getElementDoubleAbs(iter.index) == n)<a name="line.2039"></a>
<span class="sourceLineNo">2040</span>                                break;<a name="line.2040"></a>
<span class="sourceLineNo">2041</span>                }<a name="line.2041"></a>
<span class="sourceLineNo">2042</span><a name="line.2042"></a>
<span class="sourceLineNo">2043</span>                return iter.index;<a name="line.2043"></a>
<span class="sourceLineNo">2044</span>        }<a name="line.2044"></a>
<span class="sourceLineNo">2045</span><a name="line.2045"></a>
<span class="sourceLineNo">2046</span>        /**<a name="line.2046"></a>
<span class="sourceLineNo">2047</span>         * Find absolute index of first value in dataset that is greater than given number<a name="line.2047"></a>
<span class="sourceLineNo">2048</span>         * @param a<a name="line.2048"></a>
<span class="sourceLineNo">2049</span>         * @param n<a name="line.2049"></a>
<span class="sourceLineNo">2050</span>         * @return absolute index (if greater than a.getSize() then no value found)<a name="line.2050"></a>
<span class="sourceLineNo">2051</span>         */<a name="line.2051"></a>
<span class="sourceLineNo">2052</span>        public static int findIndexGreaterThan(final Dataset a, final double n) {<a name="line.2052"></a>
<span class="sourceLineNo">2053</span>                IndexIterator iter = a.getIterator();<a name="line.2053"></a>
<span class="sourceLineNo">2054</span>                while (iter.hasNext()) {<a name="line.2054"></a>
<span class="sourceLineNo">2055</span>                        if (a.getElementDoubleAbs(iter.index) &gt; n)<a name="line.2055"></a>
<span class="sourceLineNo">2056</span>                                break;<a name="line.2056"></a>
<span class="sourceLineNo">2057</span>                }<a name="line.2057"></a>
<span class="sourceLineNo">2058</span><a name="line.2058"></a>
<span class="sourceLineNo">2059</span>                return iter.index;<a name="line.2059"></a>
<span class="sourceLineNo">2060</span>        }<a name="line.2060"></a>
<span class="sourceLineNo">2061</span><a name="line.2061"></a>
<span class="sourceLineNo">2062</span>        /**<a name="line.2062"></a>
<span class="sourceLineNo">2063</span>         * Find absolute index of first value in dataset that is greater than or equal to given number<a name="line.2063"></a>
<span class="sourceLineNo">2064</span>         * @param a<a name="line.2064"></a>
<span class="sourceLineNo">2065</span>         * @param n<a name="line.2065"></a>
<span class="sourceLineNo">2066</span>         * @return absolute index (if greater than a.getSize() then no value found)<a name="line.2066"></a>
<span class="sourceLineNo">2067</span>         */<a name="line.2067"></a>
<span class="sourceLineNo">2068</span>        public static int findIndexGreaterThanOrEqualTo(final Dataset a, final double n) {<a name="line.2068"></a>
<span class="sourceLineNo">2069</span>                IndexIterator iter = a.getIterator();<a name="line.2069"></a>
<span class="sourceLineNo">2070</span>                while (iter.hasNext()) {<a name="line.2070"></a>
<span class="sourceLineNo">2071</span>                        if (a.getElementDoubleAbs(iter.index) &gt;= n)<a name="line.2071"></a>
<span class="sourceLineNo">2072</span>                                break;<a name="line.2072"></a>
<span class="sourceLineNo">2073</span>                }<a name="line.2073"></a>
<span class="sourceLineNo">2074</span><a name="line.2074"></a>
<span class="sourceLineNo">2075</span>                return iter.index;<a name="line.2075"></a>
<span class="sourceLineNo">2076</span>        }<a name="line.2076"></a>
<span class="sourceLineNo">2077</span><a name="line.2077"></a>
<span class="sourceLineNo">2078</span>        /**<a name="line.2078"></a>
<span class="sourceLineNo">2079</span>         * Find absolute index of first value in dataset that is less than given number<a name="line.2079"></a>
<span class="sourceLineNo">2080</span>         * @param a<a name="line.2080"></a>
<span class="sourceLineNo">2081</span>         * @param n<a name="line.2081"></a>
<span class="sourceLineNo">2082</span>         * @return absolute index (if greater than a.getSize() then no value found)<a name="line.2082"></a>
<span class="sourceLineNo">2083</span>         */<a name="line.2083"></a>
<span class="sourceLineNo">2084</span>        public static int findIndexLessThan(final Dataset a, final double n) {<a name="line.2084"></a>
<span class="sourceLineNo">2085</span>                IndexIterator iter = a.getIterator();<a name="line.2085"></a>
<span class="sourceLineNo">2086</span>                while (iter.hasNext()) {<a name="line.2086"></a>
<span class="sourceLineNo">2087</span>                        if (a.getElementDoubleAbs(iter.index) &lt; n)<a name="line.2087"></a>
<span class="sourceLineNo">2088</span>                                break;<a name="line.2088"></a>
<span class="sourceLineNo">2089</span>                }<a name="line.2089"></a>
<span class="sourceLineNo">2090</span><a name="line.2090"></a>
<span class="sourceLineNo">2091</span>                return iter.index;<a name="line.2091"></a>
<span class="sourceLineNo">2092</span>        }<a name="line.2092"></a>
<span class="sourceLineNo">2093</span><a name="line.2093"></a>
<span class="sourceLineNo">2094</span>        /**<a name="line.2094"></a>
<span class="sourceLineNo">2095</span>         * Find absolute index of first value in dataset that is less than or equal to given number<a name="line.2095"></a>
<span class="sourceLineNo">2096</span>         * @param a<a name="line.2096"></a>
<span class="sourceLineNo">2097</span>         * @param n<a name="line.2097"></a>
<span class="sourceLineNo">2098</span>         * @return absolute index (if greater than a.getSize() then no value found)<a name="line.2098"></a>
<span class="sourceLineNo">2099</span>         */<a name="line.2099"></a>
<span class="sourceLineNo">2100</span>        public static int findIndexLessThanOrEqualTo(final Dataset a, final double n) {<a name="line.2100"></a>
<span class="sourceLineNo">2101</span>                IndexIterator iter = a.getIterator();<a name="line.2101"></a>
<span class="sourceLineNo">2102</span>                while (iter.hasNext()) {<a name="line.2102"></a>
<span class="sourceLineNo">2103</span>                        if (a.getElementDoubleAbs(iter.index) &lt;= n)<a name="line.2103"></a>
<span class="sourceLineNo">2104</span>                                break;<a name="line.2104"></a>
<span class="sourceLineNo">2105</span>                }<a name="line.2105"></a>
<span class="sourceLineNo">2106</span><a name="line.2106"></a>
<span class="sourceLineNo">2107</span>                return iter.index;<a name="line.2107"></a>
<span class="sourceLineNo">2108</span>        }<a name="line.2108"></a>
<span class="sourceLineNo">2109</span><a name="line.2109"></a>
<span class="sourceLineNo">2110</span>        /**<a name="line.2110"></a>
<span class="sourceLineNo">2111</span>         * Find first occurrences in one dataset of values given in another sorted dataset<a name="line.2111"></a>
<span class="sourceLineNo">2112</span>         * @param a<a name="line.2112"></a>
<span class="sourceLineNo">2113</span>         * @param values sorted 1D dataset of values to find<a name="line.2113"></a>
<span class="sourceLineNo">2114</span>         * @return absolute indexes of those first occurrences (-1 is used to indicate value not found)<a name="line.2114"></a>
<span class="sourceLineNo">2115</span>         */<a name="line.2115"></a>
<span class="sourceLineNo">2116</span>        public static IntegerDataset findFirstOccurrences(final Dataset a, final Dataset values) {<a name="line.2116"></a>
<span class="sourceLineNo">2117</span>                if (values.getRank() != 1) {<a name="line.2117"></a>
<span class="sourceLineNo">2118</span>                        throw new IllegalArgumentException("Values dataset must be 1D");<a name="line.2118"></a>
<span class="sourceLineNo">2119</span>                }<a name="line.2119"></a>
<span class="sourceLineNo">2120</span>                IntegerDataset indexes = new IntegerDataset(values.getSize());<a name="line.2120"></a>
<span class="sourceLineNo">2121</span>                indexes.fill(-1);<a name="line.2121"></a>
<span class="sourceLineNo">2122</span><a name="line.2122"></a>
<span class="sourceLineNo">2123</span>                IndexIterator it = a.getIterator();<a name="line.2123"></a>
<span class="sourceLineNo">2124</span>                final int n = values.getSize();<a name="line.2124"></a>
<span class="sourceLineNo">2125</span>                if (values.getDType() == Dataset.INT64) {<a name="line.2125"></a>
<span class="sourceLineNo">2126</span>                        while (it.hasNext()) {<a name="line.2126"></a>
<span class="sourceLineNo">2127</span>                                long x = a.getElementLongAbs(it.index);<a name="line.2127"></a>
<span class="sourceLineNo">2128</span><a name="line.2128"></a>
<span class="sourceLineNo">2129</span>                                int l = 0; // binary search to find value in sorted dataset<a name="line.2129"></a>
<span class="sourceLineNo">2130</span>                                long vl = values.getLong(l);<a name="line.2130"></a>
<span class="sourceLineNo">2131</span>                                if (x &lt;= vl) {<a name="line.2131"></a>
<span class="sourceLineNo">2132</span>                                        if (x == vl &amp;&amp; indexes.getAbs(l) &lt; 0)<a name="line.2132"></a>
<span class="sourceLineNo">2133</span>                                                indexes.setAbs(l, it.index);<a name="line.2133"></a>
<span class="sourceLineNo">2134</span>                                        continue;<a name="line.2134"></a>
<span class="sourceLineNo">2135</span>                                }<a name="line.2135"></a>
<span class="sourceLineNo">2136</span>                                int h = n - 1;<a name="line.2136"></a>
<span class="sourceLineNo">2137</span>                                long vh = values.getLong(h);<a name="line.2137"></a>
<span class="sourceLineNo">2138</span>                                if (x &gt;= vh) {<a name="line.2138"></a>
<span class="sourceLineNo">2139</span>                                        if (x == vh &amp;&amp; indexes.getAbs(h) &lt; 0)<a name="line.2139"></a>
<span class="sourceLineNo">2140</span>                                                indexes.setAbs(h, it.index);<a name="line.2140"></a>
<span class="sourceLineNo">2141</span>                                        continue;<a name="line.2141"></a>
<span class="sourceLineNo">2142</span>                                }<a name="line.2142"></a>
<span class="sourceLineNo">2143</span>                                while (h - l &gt; 1) {<a name="line.2143"></a>
<span class="sourceLineNo">2144</span>                                        int m = (l + h) / 2;<a name="line.2144"></a>
<span class="sourceLineNo">2145</span>                                        long vm = values.getLong(m);<a name="line.2145"></a>
<span class="sourceLineNo">2146</span>                                        if (x &lt; vm) {<a name="line.2146"></a>
<span class="sourceLineNo">2147</span>                                                h = m;<a name="line.2147"></a>
<span class="sourceLineNo">2148</span>                                        } else if (x &gt; vm) {<a name="line.2148"></a>
<span class="sourceLineNo">2149</span>                                                l = m;<a name="line.2149"></a>
<span class="sourceLineNo">2150</span>                                        } else {<a name="line.2150"></a>
<span class="sourceLineNo">2151</span>                                                if (indexes.getAbs(m) &lt; 0)<a name="line.2151"></a>
<span class="sourceLineNo">2152</span>                                                        indexes.setAbs(m, it.index);<a name="line.2152"></a>
<span class="sourceLineNo">2153</span>                                                break;<a name="line.2153"></a>
<span class="sourceLineNo">2154</span>                                        }<a name="line.2154"></a>
<span class="sourceLineNo">2155</span>                                }<a name="line.2155"></a>
<span class="sourceLineNo">2156</span>                        }<a name="line.2156"></a>
<span class="sourceLineNo">2157</span>                } else {<a name="line.2157"></a>
<span class="sourceLineNo">2158</span>                        while (it.hasNext()) {<a name="line.2158"></a>
<span class="sourceLineNo">2159</span>                                double x = a.getElementDoubleAbs(it.index);<a name="line.2159"></a>
<span class="sourceLineNo">2160</span><a name="line.2160"></a>
<span class="sourceLineNo">2161</span>                                int l = 0; // binary search to find value in sorted dataset<a name="line.2161"></a>
<span class="sourceLineNo">2162</span>                                double vl = values.getDouble(l);<a name="line.2162"></a>
<span class="sourceLineNo">2163</span>                                if (x &lt;= vl) {<a name="line.2163"></a>
<span class="sourceLineNo">2164</span>                                        if (x == vl &amp;&amp; indexes.getAbs(l) &lt; 0)<a name="line.2164"></a>
<span class="sourceLineNo">2165</span>                                                indexes.setAbs(l, it.index);<a name="line.2165"></a>
<span class="sourceLineNo">2166</span>                                        continue;<a name="line.2166"></a>
<span class="sourceLineNo">2167</span>                                }<a name="line.2167"></a>
<span class="sourceLineNo">2168</span>                                int h = n - 1;<a name="line.2168"></a>
<span class="sourceLineNo">2169</span>                                double vh = values.getDouble(h);<a name="line.2169"></a>
<span class="sourceLineNo">2170</span>                                if (x &gt;= vh) {<a name="line.2170"></a>
<span class="sourceLineNo">2171</span>                                        if (x == vh &amp;&amp; indexes.getAbs(h) &lt; 0)<a name="line.2171"></a>
<span class="sourceLineNo">2172</span>                                                indexes.setAbs(h, it.index);<a name="line.2172"></a>
<span class="sourceLineNo">2173</span>                                        continue;<a name="line.2173"></a>
<span class="sourceLineNo">2174</span>                                }<a name="line.2174"></a>
<span class="sourceLineNo">2175</span>                                while (h - l &gt; 1) {<a name="line.2175"></a>
<span class="sourceLineNo">2176</span>                                        int m = (l + h) / 2;<a name="line.2176"></a>
<span class="sourceLineNo">2177</span>                                        double vm = values.getDouble(m);<a name="line.2177"></a>
<span class="sourceLineNo">2178</span>                                        if (x &lt; vm) {<a name="line.2178"></a>
<span class="sourceLineNo">2179</span>                                                h = m;<a name="line.2179"></a>
<span class="sourceLineNo">2180</span>                                        } else if (x &gt; vm) {<a name="line.2180"></a>
<span class="sourceLineNo">2181</span>                                                l = m;<a name="line.2181"></a>
<span class="sourceLineNo">2182</span>                                        } else {<a name="line.2182"></a>
<span class="sourceLineNo">2183</span>                                                if (indexes.getAbs(m) &lt; 0)<a name="line.2183"></a>
<span class="sourceLineNo">2184</span>                                                        indexes.setAbs(m, it.index);<a name="line.2184"></a>
<span class="sourceLineNo">2185</span>                                                break;<a name="line.2185"></a>
<span class="sourceLineNo">2186</span>                                        }<a name="line.2186"></a>
<span class="sourceLineNo">2187</span>                                }<a name="line.2187"></a>
<span class="sourceLineNo">2188</span>                        }<a name="line.2188"></a>
<span class="sourceLineNo">2189</span>                }<a name="line.2189"></a>
<span class="sourceLineNo">2190</span>                return indexes;<a name="line.2190"></a>
<span class="sourceLineNo">2191</span>        }<a name="line.2191"></a>
<span class="sourceLineNo">2192</span><a name="line.2192"></a>
<span class="sourceLineNo">2193</span>        /**<a name="line.2193"></a>
<span class="sourceLineNo">2194</span>         * Find indexes in sorted dataset of values for each value in other dataset<a name="line.2194"></a>
<span class="sourceLineNo">2195</span>         * @param a<a name="line.2195"></a>
<span class="sourceLineNo">2196</span>         * @param values sorted 1D dataset of values to find<a name="line.2196"></a>
<span class="sourceLineNo">2197</span>         * @return absolute indexes of values (-1 is used to indicate value not found)<a name="line.2197"></a>
<span class="sourceLineNo">2198</span>         */<a name="line.2198"></a>
<span class="sourceLineNo">2199</span>        public static IntegerDataset findIndexesForValues(final Dataset a, final Dataset values) {<a name="line.2199"></a>
<span class="sourceLineNo">2200</span>                if (values.getRank() != 1) {<a name="line.2200"></a>
<span class="sourceLineNo">2201</span>                        throw new IllegalArgumentException("Values dataset must be 1D");<a name="line.2201"></a>
<span class="sourceLineNo">2202</span>                }<a name="line.2202"></a>
<span class="sourceLineNo">2203</span>                IntegerDataset indexes = new IntegerDataset(a.getSize());<a name="line.2203"></a>
<span class="sourceLineNo">2204</span>                indexes.fill(-1);<a name="line.2204"></a>
<span class="sourceLineNo">2205</span><a name="line.2205"></a>
<span class="sourceLineNo">2206</span>                IndexIterator it = a.getIterator();<a name="line.2206"></a>
<span class="sourceLineNo">2207</span>                int i = -1;<a name="line.2207"></a>
<span class="sourceLineNo">2208</span>                final int n = values.getSize();<a name="line.2208"></a>
<span class="sourceLineNo">2209</span>                if (values.getDType() == Dataset.INT64) {<a name="line.2209"></a>
<span class="sourceLineNo">2210</span>                        while (it.hasNext()) {<a name="line.2210"></a>
<span class="sourceLineNo">2211</span>                                i++;<a name="line.2211"></a>
<span class="sourceLineNo">2212</span>                                long x = a.getElementLongAbs(it.index);<a name="line.2212"></a>
<span class="sourceLineNo">2213</span><a name="line.2213"></a>
<span class="sourceLineNo">2214</span>                                int l = 0; // binary search to find value in sorted dataset<a name="line.2214"></a>
<span class="sourceLineNo">2215</span>                                long vl = values.getLong(l);<a name="line.2215"></a>
<span class="sourceLineNo">2216</span>                                if (x &lt;= vl) {<a name="line.2216"></a>
<span class="sourceLineNo">2217</span>                                        if (x == vl)<a name="line.2217"></a>
<span class="sourceLineNo">2218</span>                                                indexes.setAbs(i, l);<a name="line.2218"></a>
<span class="sourceLineNo">2219</span>                                        continue;<a name="line.2219"></a>
<span class="sourceLineNo">2220</span>                                }<a name="line.2220"></a>
<span class="sourceLineNo">2221</span>                                int h = n - 1;<a name="line.2221"></a>
<span class="sourceLineNo">2222</span>                                long vh = values.getLong(h);<a name="line.2222"></a>
<span class="sourceLineNo">2223</span>                                if (x &gt;= vh) {<a name="line.2223"></a>
<span class="sourceLineNo">2224</span>                                        if (x == vh)<a name="line.2224"></a>
<span class="sourceLineNo">2225</span>                                                indexes.setAbs(i, h);<a name="line.2225"></a>
<span class="sourceLineNo">2226</span>                                        continue;<a name="line.2226"></a>
<span class="sourceLineNo">2227</span>                                }<a name="line.2227"></a>
<span class="sourceLineNo">2228</span>                                while (h - l &gt; 1) {<a name="line.2228"></a>
<span class="sourceLineNo">2229</span>                                        int m = (l + h) / 2;<a name="line.2229"></a>
<span class="sourceLineNo">2230</span>                                        long vm = values.getLong(m);<a name="line.2230"></a>
<span class="sourceLineNo">2231</span>                                        if (x &lt; vm) {<a name="line.2231"></a>
<span class="sourceLineNo">2232</span>                                                h = m;<a name="line.2232"></a>
<span class="sourceLineNo">2233</span>                                        } else if (x &gt; vm) {<a name="line.2233"></a>
<span class="sourceLineNo">2234</span>                                                l = m;<a name="line.2234"></a>
<span class="sourceLineNo">2235</span>                                        } else {<a name="line.2235"></a>
<span class="sourceLineNo">2236</span>                                                indexes.setAbs(i, m);<a name="line.2236"></a>
<span class="sourceLineNo">2237</span>                                                break;<a name="line.2237"></a>
<span class="sourceLineNo">2238</span>                                        }<a name="line.2238"></a>
<span class="sourceLineNo">2239</span>                                }<a name="line.2239"></a>
<span class="sourceLineNo">2240</span>                        }<a name="line.2240"></a>
<span class="sourceLineNo">2241</span>                } else {<a name="line.2241"></a>
<span class="sourceLineNo">2242</span>                        while (it.hasNext()) {<a name="line.2242"></a>
<span class="sourceLineNo">2243</span>                                i++;<a name="line.2243"></a>
<span class="sourceLineNo">2244</span>                                double x = a.getElementDoubleAbs(it.index);<a name="line.2244"></a>
<span class="sourceLineNo">2245</span><a name="line.2245"></a>
<span class="sourceLineNo">2246</span>                                int l = 0; // binary search to find value in sorted dataset<a name="line.2246"></a>
<span class="sourceLineNo">2247</span>                                double vl = values.getDouble(l);<a name="line.2247"></a>
<span class="sourceLineNo">2248</span>                                if (x &lt;= vl) {<a name="line.2248"></a>
<span class="sourceLineNo">2249</span>                                        if (x == vl)<a name="line.2249"></a>
<span class="sourceLineNo">2250</span>                                                indexes.setAbs(i, l);<a name="line.2250"></a>
<span class="sourceLineNo">2251</span>                                        continue;<a name="line.2251"></a>
<span class="sourceLineNo">2252</span>                                }<a name="line.2252"></a>
<span class="sourceLineNo">2253</span>                                int h = n - 1;<a name="line.2253"></a>
<span class="sourceLineNo">2254</span>                                double vh = values.getDouble(h);<a name="line.2254"></a>
<span class="sourceLineNo">2255</span>                                if (x &gt;= vh) {<a name="line.2255"></a>
<span class="sourceLineNo">2256</span>                                        if (x == vh)<a name="line.2256"></a>
<span class="sourceLineNo">2257</span>                                                indexes.setAbs(i, h);<a name="line.2257"></a>
<span class="sourceLineNo">2258</span>                                        continue;<a name="line.2258"></a>
<span class="sourceLineNo">2259</span>                                }<a name="line.2259"></a>
<span class="sourceLineNo">2260</span>                                while (h - l &gt; 1) {<a name="line.2260"></a>
<span class="sourceLineNo">2261</span>                                        int m = (l + h) / 2;<a name="line.2261"></a>
<span class="sourceLineNo">2262</span>                                        double vm = values.getDouble(m);<a name="line.2262"></a>
<span class="sourceLineNo">2263</span>                                        if (x &lt; vm) {<a name="line.2263"></a>
<span class="sourceLineNo">2264</span>                                                h = m;<a name="line.2264"></a>
<span class="sourceLineNo">2265</span>                                        } else if (x &gt; vm) {<a name="line.2265"></a>
<span class="sourceLineNo">2266</span>                                                l = m;<a name="line.2266"></a>
<span class="sourceLineNo">2267</span>                                        } else {<a name="line.2267"></a>
<span class="sourceLineNo">2268</span>                                                indexes.setAbs(i, m);<a name="line.2268"></a>
<span class="sourceLineNo">2269</span>                                                break;<a name="line.2269"></a>
<span class="sourceLineNo">2270</span>                                        }<a name="line.2270"></a>
<span class="sourceLineNo">2271</span>                                }<a name="line.2271"></a>
<span class="sourceLineNo">2272</span>                        }<a name="line.2272"></a>
<span class="sourceLineNo">2273</span>                }<a name="line.2273"></a>
<span class="sourceLineNo">2274</span><a name="line.2274"></a>
<span class="sourceLineNo">2275</span>                return indexes;<a name="line.2275"></a>
<span class="sourceLineNo">2276</span>        }<a name="line.2276"></a>
<span class="sourceLineNo">2277</span><a name="line.2277"></a>
<span class="sourceLineNo">2278</span>        /**<a name="line.2278"></a>
<span class="sourceLineNo">2279</span>         * Roll items over given axis by given amount<a name="line.2279"></a>
<span class="sourceLineNo">2280</span>         * @param a<a name="line.2280"></a>
<span class="sourceLineNo">2281</span>         * @param shift<a name="line.2281"></a>
<span class="sourceLineNo">2282</span>         * @param axis if null, then roll flattened dataset<a name="line.2282"></a>
<span class="sourceLineNo">2283</span>         * @return rolled dataset<a name="line.2283"></a>
<span class="sourceLineNo">2284</span>         */<a name="line.2284"></a>
<span class="sourceLineNo">2285</span>        public static &lt;T extends Dataset&gt; T roll(final T a, final int shift, Integer axis) {<a name="line.2285"></a>
<span class="sourceLineNo">2286</span>                Dataset r = DatasetFactory.zeros(a);<a name="line.2286"></a>
<span class="sourceLineNo">2287</span>                int is = a.getElementsPerItem();<a name="line.2287"></a>
<span class="sourceLineNo">2288</span>                if (axis == null) {<a name="line.2288"></a>
<span class="sourceLineNo">2289</span>                        IndexIterator it = a.getIterator();<a name="line.2289"></a>
<span class="sourceLineNo">2290</span>                        int s = r.getSize();<a name="line.2290"></a>
<span class="sourceLineNo">2291</span>                        int i = shift % s;<a name="line.2291"></a>
<span class="sourceLineNo">2292</span>                        if (i &lt; 0)<a name="line.2292"></a>
<span class="sourceLineNo">2293</span>                                i += s;<a name="line.2293"></a>
<span class="sourceLineNo">2294</span>                        while (it.hasNext()) {<a name="line.2294"></a>
<span class="sourceLineNo">2295</span>                                r.setObjectAbs(i, a.getObjectAbs(it.index));<a name="line.2295"></a>
<span class="sourceLineNo">2296</span>                                i += is;<a name="line.2296"></a>
<span class="sourceLineNo">2297</span>                                if (i &gt;= s) {<a name="line.2297"></a>
<span class="sourceLineNo">2298</span>                                        i %= s;<a name="line.2298"></a>
<span class="sourceLineNo">2299</span>                                }<a name="line.2299"></a>
<span class="sourceLineNo">2300</span>                        }<a name="line.2300"></a>
<span class="sourceLineNo">2301</span>                } else {<a name="line.2301"></a>
<span class="sourceLineNo">2302</span>                        axis = a.checkAxis(axis);<a name="line.2302"></a>
<span class="sourceLineNo">2303</span>                        PositionIterator pi = a.getPositionIterator(axis);<a name="line.2303"></a>
<span class="sourceLineNo">2304</span>                        int s = a.getShapeRef()[axis];<a name="line.2304"></a>
<span class="sourceLineNo">2305</span>                        Dataset u = DatasetFactory.zeros(is, a.getClass(), new int[] {s});<a name="line.2305"></a>
<span class="sourceLineNo">2306</span>                        Dataset v = DatasetFactory.zeros(u);<a name="line.2306"></a>
<span class="sourceLineNo">2307</span>                        int[] pos = pi.getPos();<a name="line.2307"></a>
<span class="sourceLineNo">2308</span>                        boolean[] hit = pi.getOmit();<a name="line.2308"></a>
<span class="sourceLineNo">2309</span>                        while (pi.hasNext()) {<a name="line.2309"></a>
<span class="sourceLineNo">2310</span>                                a.copyItemsFromAxes(pos, hit, u);<a name="line.2310"></a>
<span class="sourceLineNo">2311</span>                                int i = shift % s;<a name="line.2311"></a>
<span class="sourceLineNo">2312</span>                                if (i &lt; 0)<a name="line.2312"></a>
<span class="sourceLineNo">2313</span>                                        i += s;<a name="line.2313"></a>
<span class="sourceLineNo">2314</span>                                for (int j = 0; j &lt; s; j++) {<a name="line.2314"></a>
<span class="sourceLineNo">2315</span>                                        v.setObjectAbs(i, u.getObjectAbs(j*is));<a name="line.2315"></a>
<span class="sourceLineNo">2316</span>                                        i += is;<a name="line.2316"></a>
<span class="sourceLineNo">2317</span>                                        if (i &gt;= s) {<a name="line.2317"></a>
<span class="sourceLineNo">2318</span>                                                i %= s;<a name="line.2318"></a>
<span class="sourceLineNo">2319</span>                                        }<a name="line.2319"></a>
<span class="sourceLineNo">2320</span>                                }<a name="line.2320"></a>
<span class="sourceLineNo">2321</span>                                r.setItemsOnAxes(pos, hit, v.getBuffer());<a name="line.2321"></a>
<span class="sourceLineNo">2322</span>                        }<a name="line.2322"></a>
<span class="sourceLineNo">2323</span>                }<a name="line.2323"></a>
<span class="sourceLineNo">2324</span>                return (T) r;<a name="line.2324"></a>
<span class="sourceLineNo">2325</span>        }<a name="line.2325"></a>
<span class="sourceLineNo">2326</span><a name="line.2326"></a>
<span class="sourceLineNo">2327</span>        /**<a name="line.2327"></a>
<span class="sourceLineNo">2328</span>         * Roll the specified axis backwards until it lies in given position<a name="line.2328"></a>
<span class="sourceLineNo">2329</span>         * @param a<a name="line.2329"></a>
<span class="sourceLineNo">2330</span>         * @param axis The rolled axis (index in shape array). Other axes are left unchanged in relative positions<a name="line.2330"></a>
<span class="sourceLineNo">2331</span>         * @param start The position with it right of the destination of the rolled axis<a name="line.2331"></a>
<span class="sourceLineNo">2332</span>         * @return dataset with rolled axis<a name="line.2332"></a>
<span class="sourceLineNo">2333</span>         */<a name="line.2333"></a>
<span class="sourceLineNo">2334</span>        public static &lt;T extends Dataset&gt; T rollAxis(final T a, int axis, int start) {<a name="line.2334"></a>
<span class="sourceLineNo">2335</span>                int r = a.getRank();<a name="line.2335"></a>
<span class="sourceLineNo">2336</span>                axis = a.checkAxis(axis);<a name="line.2336"></a>
<span class="sourceLineNo">2337</span>                if (start &lt; 0)<a name="line.2337"></a>
<span class="sourceLineNo">2338</span>                        start += r;<a name="line.2338"></a>
<span class="sourceLineNo">2339</span>                if (start &lt; 0 || start &gt; r) {<a name="line.2339"></a>
<span class="sourceLineNo">2340</span>                        throw new IllegalArgumentException("Start is out of range: it should be &gt;= 0 and &lt;= " + r);<a name="line.2340"></a>
<span class="sourceLineNo">2341</span>                }<a name="line.2341"></a>
<span class="sourceLineNo">2342</span>                if (axis &lt; start)<a name="line.2342"></a>
<span class="sourceLineNo">2343</span>                        start--;<a name="line.2343"></a>
<span class="sourceLineNo">2344</span><a name="line.2344"></a>
<span class="sourceLineNo">2345</span>                if (axis == start)<a name="line.2345"></a>
<span class="sourceLineNo">2346</span>                        return a;<a name="line.2346"></a>
<span class="sourceLineNo">2347</span><a name="line.2347"></a>
<span class="sourceLineNo">2348</span>                ArrayList&lt;Integer&gt; axes = new ArrayList&lt;Integer&gt;();<a name="line.2348"></a>
<span class="sourceLineNo">2349</span>                for (int i = 0; i &lt; r; i++) {<a name="line.2349"></a>
<span class="sourceLineNo">2350</span>                        if (i != axis) {<a name="line.2350"></a>
<span class="sourceLineNo">2351</span>                                axes.add(i);<a name="line.2351"></a>
<span class="sourceLineNo">2352</span>                        }<a name="line.2352"></a>
<span class="sourceLineNo">2353</span>                }<a name="line.2353"></a>
<span class="sourceLineNo">2354</span>                axes.add(start, axis);<a name="line.2354"></a>
<span class="sourceLineNo">2355</span>                int[] aa = new int[r];<a name="line.2355"></a>
<span class="sourceLineNo">2356</span>                for (int i = 0; i &lt; r; i++) {<a name="line.2356"></a>
<span class="sourceLineNo">2357</span>                        aa[i] = axes.get(i);<a name="line.2357"></a>
<span class="sourceLineNo">2358</span>                }<a name="line.2358"></a>
<span class="sourceLineNo">2359</span>                return (T) a.getTransposedView(aa);<a name="line.2359"></a>
<span class="sourceLineNo">2360</span>        }<a name="line.2360"></a>
<span class="sourceLineNo">2361</span><a name="line.2361"></a>
<span class="sourceLineNo">2362</span>        private static SliceND createFlippedSlice(final Dataset a, int axis) {<a name="line.2362"></a>
<span class="sourceLineNo">2363</span>                int[] shape = a.getShapeRef();<a name="line.2363"></a>
<span class="sourceLineNo">2364</span>                SliceND slice = new SliceND(shape);<a name="line.2364"></a>
<span class="sourceLineNo">2365</span>                slice.flip(axis);<a name="line.2365"></a>
<span class="sourceLineNo">2366</span>                return slice;<a name="line.2366"></a>
<span class="sourceLineNo">2367</span>        }<a name="line.2367"></a>
<span class="sourceLineNo">2368</span><a name="line.2368"></a>
<span class="sourceLineNo">2369</span>        /**<a name="line.2369"></a>
<span class="sourceLineNo">2370</span>         * Flip items in left/right direction, column-wise, or along second axis<a name="line.2370"></a>
<span class="sourceLineNo">2371</span>         * @param a dataset must be at least 2D<a name="line.2371"></a>
<span class="sourceLineNo">2372</span>         * @return view of flipped dataset<a name="line.2372"></a>
<span class="sourceLineNo">2373</span>         */<a name="line.2373"></a>
<span class="sourceLineNo">2374</span>        public static &lt;T extends Dataset&gt; T flipLeftRight(final T a) {<a name="line.2374"></a>
<span class="sourceLineNo">2375</span>                if (a.getRank() &lt; 2) {<a name="line.2375"></a>
<span class="sourceLineNo">2376</span>                        throw new IllegalArgumentException("Dataset must be at least 2D");<a name="line.2376"></a>
<span class="sourceLineNo">2377</span>                }<a name="line.2377"></a>
<span class="sourceLineNo">2378</span>                return (T) a.getSliceView(createFlippedSlice(a, 1));<a name="line.2378"></a>
<span class="sourceLineNo">2379</span>        }<a name="line.2379"></a>
<span class="sourceLineNo">2380</span><a name="line.2380"></a>
<span class="sourceLineNo">2381</span>        /**<a name="line.2381"></a>
<span class="sourceLineNo">2382</span>         * Flip items in up/down direction, row-wise, or along first axis<a name="line.2382"></a>
<span class="sourceLineNo">2383</span>         * @param a dataset<a name="line.2383"></a>
<span class="sourceLineNo">2384</span>         * @return view of flipped dataset<a name="line.2384"></a>
<span class="sourceLineNo">2385</span>         */<a name="line.2385"></a>
<span class="sourceLineNo">2386</span>        public static &lt;T extends Dataset&gt; T flipUpDown(final T a) {<a name="line.2386"></a>
<span class="sourceLineNo">2387</span>                return (T) a.getSliceView(createFlippedSlice(a, 0));<a name="line.2387"></a>
<span class="sourceLineNo">2388</span>        }<a name="line.2388"></a>
<span class="sourceLineNo">2389</span><a name="line.2389"></a>
<span class="sourceLineNo">2390</span>        /**<a name="line.2390"></a>
<span class="sourceLineNo">2391</span>         * Rotate items in first two dimension by 90 degrees anti-clockwise<a name="line.2391"></a>
<span class="sourceLineNo">2392</span>         * @param a dataset must be at least 2D<a name="line.2392"></a>
<span class="sourceLineNo">2393</span>         * @return view of flipped dataset<a name="line.2393"></a>
<span class="sourceLineNo">2394</span>         */<a name="line.2394"></a>
<span class="sourceLineNo">2395</span>        public static &lt;T extends Dataset&gt; T rotate90(final T a) {<a name="line.2395"></a>
<span class="sourceLineNo">2396</span>                return rotate90(a, 1);<a name="line.2396"></a>
<span class="sourceLineNo">2397</span>        }<a name="line.2397"></a>
<span class="sourceLineNo">2398</span><a name="line.2398"></a>
<span class="sourceLineNo">2399</span>        /**<a name="line.2399"></a>
<span class="sourceLineNo">2400</span>         * Rotate items in first two dimension by 90 degrees anti-clockwise<a name="line.2400"></a>
<span class="sourceLineNo">2401</span>         * @param a dataset must be at least 2D<a name="line.2401"></a>
<span class="sourceLineNo">2402</span>         * @param k number of 90-degree rotations<a name="line.2402"></a>
<span class="sourceLineNo">2403</span>         * @return view of flipped dataset<a name="line.2403"></a>
<span class="sourceLineNo">2404</span>         */<a name="line.2404"></a>
<span class="sourceLineNo">2405</span>        public static &lt;T extends Dataset&gt; T rotate90(final T a, int k) {<a name="line.2405"></a>
<span class="sourceLineNo">2406</span>                k = k % 4;<a name="line.2406"></a>
<span class="sourceLineNo">2407</span>                while (k &lt; 0) {<a name="line.2407"></a>
<span class="sourceLineNo">2408</span>                        k += 4;<a name="line.2408"></a>
<span class="sourceLineNo">2409</span>                }<a name="line.2409"></a>
<span class="sourceLineNo">2410</span>                int r = a.getRank();<a name="line.2410"></a>
<span class="sourceLineNo">2411</span>                if (r &lt; 2) {<a name="line.2411"></a>
<span class="sourceLineNo">2412</span>                        throw new IllegalArgumentException("Dataset must be at least 2D");<a name="line.2412"></a>
<span class="sourceLineNo">2413</span>                }<a name="line.2413"></a>
<span class="sourceLineNo">2414</span>                switch (k) {<a name="line.2414"></a>
<span class="sourceLineNo">2415</span>                case 1: case 3:<a name="line.2415"></a>
<span class="sourceLineNo">2416</span>                        int[] axes = new int[r];<a name="line.2416"></a>
<span class="sourceLineNo">2417</span>                        axes[0] = 1;<a name="line.2417"></a>
<span class="sourceLineNo">2418</span>                        axes[1] = 0;<a name="line.2418"></a>
<span class="sourceLineNo">2419</span>                        for (int i = 2; i &lt; r; i++) {<a name="line.2419"></a>
<span class="sourceLineNo">2420</span>                                axes[i] = i;<a name="line.2420"></a>
<span class="sourceLineNo">2421</span>                        }<a name="line.2421"></a>
<span class="sourceLineNo">2422</span>                        Dataset t = a.getTransposedView(axes);<a name="line.2422"></a>
<span class="sourceLineNo">2423</span>                        return (T) t.getSliceView(createFlippedSlice(t, k == 1 ? 0 : 1));<a name="line.2423"></a>
<span class="sourceLineNo">2424</span>                case 2:<a name="line.2424"></a>
<span class="sourceLineNo">2425</span>                        SliceND s = createFlippedSlice(a, 0);<a name="line.2425"></a>
<span class="sourceLineNo">2426</span>                        s.flip(1);<a name="line.2426"></a>
<span class="sourceLineNo">2427</span>                        return (T) a.getSliceView(s);<a name="line.2427"></a>
<span class="sourceLineNo">2428</span>                default:<a name="line.2428"></a>
<span class="sourceLineNo">2429</span>                case 0:<a name="line.2429"></a>
<span class="sourceLineNo">2430</span>                        return a;<a name="line.2430"></a>
<span class="sourceLineNo">2431</span>                }<a name="line.2431"></a>
<span class="sourceLineNo">2432</span>        }<a name="line.2432"></a>
<span class="sourceLineNo">2433</span><a name="line.2433"></a>
<span class="sourceLineNo">2434</span>        /**<a name="line.2434"></a>
<span class="sourceLineNo">2435</span>         * Select content according where condition is true. All inputs are broadcasted to a maximum shape<a name="line.2435"></a>
<span class="sourceLineNo">2436</span>         * @param condition boolean dataset<a name="line.2436"></a>
<span class="sourceLineNo">2437</span>         * @param x<a name="line.2437"></a>
<span class="sourceLineNo">2438</span>         * @param y<a name="line.2438"></a>
<span class="sourceLineNo">2439</span>         * @return dataset where content is x or y depending on whether condition is true or otherwise<a name="line.2439"></a>
<span class="sourceLineNo">2440</span>         */<a name="line.2440"></a>
<span class="sourceLineNo">2441</span>        public static Dataset select(BooleanDataset condition, Object x, Object y) {<a name="line.2441"></a>
<span class="sourceLineNo">2442</span>                Object[] all = new Object[] {condition, x, y};<a name="line.2442"></a>
<span class="sourceLineNo">2443</span>                Dataset[] dAll = BroadcastUtils.convertAndBroadcast(all);<a name="line.2443"></a>
<span class="sourceLineNo">2444</span>                condition = (BooleanDataset) dAll[0];<a name="line.2444"></a>
<span class="sourceLineNo">2445</span>                Dataset dx = dAll[1];<a name="line.2445"></a>
<span class="sourceLineNo">2446</span>                Dataset dy = dAll[2];<a name="line.2446"></a>
<span class="sourceLineNo">2447</span>                int dt = DTypeUtils.getBestDType(dx.getDType(),dy.getDType());<a name="line.2447"></a>
<span class="sourceLineNo">2448</span>                int ds = Math.max(dx.getElementsPerItem(), dy.getElementsPerItem());<a name="line.2448"></a>
<span class="sourceLineNo">2449</span><a name="line.2449"></a>
<span class="sourceLineNo">2450</span>                @SuppressWarnings("deprecation")<a name="line.2450"></a>
<span class="sourceLineNo">2451</span>                Dataset r = DatasetFactory.zeros(ds, condition.getShapeRef(), dt);<a name="line.2451"></a>
<span class="sourceLineNo">2452</span>                IndexIterator iter = condition.getIterator(true);<a name="line.2452"></a>
<span class="sourceLineNo">2453</span>                final int[] pos = iter.getPos();<a name="line.2453"></a>
<span class="sourceLineNo">2454</span>                int i = 0;<a name="line.2454"></a>
<span class="sourceLineNo">2455</span>                while (iter.hasNext()) {<a name="line.2455"></a>
<span class="sourceLineNo">2456</span>                        r.setObjectAbs(i++, condition.getElementBooleanAbs(iter.index) ? dx.getObject(pos) : dy.getObject(pos));<a name="line.2456"></a>
<span class="sourceLineNo">2457</span>                }<a name="line.2457"></a>
<span class="sourceLineNo">2458</span>                return r;<a name="line.2458"></a>
<span class="sourceLineNo">2459</span>        }<a name="line.2459"></a>
<span class="sourceLineNo">2460</span><a name="line.2460"></a>
<span class="sourceLineNo">2461</span>        /**<a name="line.2461"></a>
<span class="sourceLineNo">2462</span>         * Select content from choices where condition is true, otherwise use default. All inputs are broadcasted to a maximum shape<a name="line.2462"></a>
<span class="sourceLineNo">2463</span>         * @param conditions array of boolean datasets<a name="line.2463"></a>
<span class="sourceLineNo">2464</span>         * @param choices array of datasets or objects<a name="line.2464"></a>
<span class="sourceLineNo">2465</span>         * @param def default value (can be a dataset)<a name="line.2465"></a>
<span class="sourceLineNo">2466</span>         * @return dataset<a name="line.2466"></a>
<span class="sourceLineNo">2467</span>         */<a name="line.2467"></a>
<span class="sourceLineNo">2468</span>        public static Dataset select(BooleanDataset[] conditions, Object[] choices, Object def) {<a name="line.2468"></a>
<span class="sourceLineNo">2469</span>                final int n = conditions.length;<a name="line.2469"></a>
<span class="sourceLineNo">2470</span>                if (choices.length != n) {<a name="line.2470"></a>
<span class="sourceLineNo">2471</span>                        throw new IllegalArgumentException("Choices list is not same length as conditions list");<a name="line.2471"></a>
<span class="sourceLineNo">2472</span>                }<a name="line.2472"></a>
<span class="sourceLineNo">2473</span>                Object[] all = new Object[2*n];<a name="line.2473"></a>
<span class="sourceLineNo">2474</span>                System.arraycopy(conditions, 0, all, 0, n);<a name="line.2474"></a>
<span class="sourceLineNo">2475</span>                System.arraycopy(choices, 0, all, n, n);<a name="line.2475"></a>
<span class="sourceLineNo">2476</span>                Dataset[] dAll = BroadcastUtils.convertAndBroadcast(all);<a name="line.2476"></a>
<span class="sourceLineNo">2477</span>                conditions = new BooleanDataset[n];<a name="line.2477"></a>
<span class="sourceLineNo">2478</span>                Dataset[] dChoices = new Dataset[n];<a name="line.2478"></a>
<span class="sourceLineNo">2479</span>                System.arraycopy(dAll, 0, conditions, 0, n);<a name="line.2479"></a>
<span class="sourceLineNo">2480</span>                System.arraycopy(dAll, n, dChoices, 0, n);<a name="line.2480"></a>
<span class="sourceLineNo">2481</span>                int dt = -1;<a name="line.2481"></a>
<span class="sourceLineNo">2482</span>                int ds = -1;<a name="line.2482"></a>
<span class="sourceLineNo">2483</span>                for (int i = 0; i &lt; n; i++) {<a name="line.2483"></a>
<span class="sourceLineNo">2484</span>                        Dataset a = dChoices[i];<a name="line.2484"></a>
<span class="sourceLineNo">2485</span>                        int t = a.getDType();<a name="line.2485"></a>
<span class="sourceLineNo">2486</span>                        if (t &gt; dt)<a name="line.2486"></a>
<span class="sourceLineNo">2487</span>                                dt = t;<a name="line.2487"></a>
<span class="sourceLineNo">2488</span>                        int s = a.getElementsPerItem();<a name="line.2488"></a>
<span class="sourceLineNo">2489</span>                        if (s &gt; ds)<a name="line.2489"></a>
<span class="sourceLineNo">2490</span>                                ds = s;<a name="line.2490"></a>
<span class="sourceLineNo">2491</span>                }<a name="line.2491"></a>
<span class="sourceLineNo">2492</span>                if (dt &lt; 0 || ds &lt; 1) {<a name="line.2492"></a>
<span class="sourceLineNo">2493</span>                        throw new IllegalArgumentException("Dataset types of choices are invalid");<a name="line.2493"></a>
<span class="sourceLineNo">2494</span>                }<a name="line.2494"></a>
<span class="sourceLineNo">2495</span><a name="line.2495"></a>
<span class="sourceLineNo">2496</span>                @SuppressWarnings("deprecation")<a name="line.2496"></a>
<span class="sourceLineNo">2497</span>                Dataset r = DatasetFactory.zeros(ds, conditions[0].getShapeRef(), dt);<a name="line.2497"></a>
<span class="sourceLineNo">2498</span>                Dataset d = DatasetFactory.createFromObject(def).getBroadcastView(r.getShapeRef());<a name="line.2498"></a>
<span class="sourceLineNo">2499</span>                PositionIterator iter = new PositionIterator(r.getShapeRef());<a name="line.2499"></a>
<span class="sourceLineNo">2500</span>                final int[] pos = iter.getPos();<a name="line.2500"></a>
<span class="sourceLineNo">2501</span>                int i = 0;<a name="line.2501"></a>
<span class="sourceLineNo">2502</span>                while (iter.hasNext()) {<a name="line.2502"></a>
<span class="sourceLineNo">2503</span>                        int j = 0;<a name="line.2503"></a>
<span class="sourceLineNo">2504</span>                        for (; j &lt; n; j++) {<a name="line.2504"></a>
<span class="sourceLineNo">2505</span>                                if (conditions[j].get(pos)) {<a name="line.2505"></a>
<span class="sourceLineNo">2506</span>                                        r.setObjectAbs(i++, dChoices[j].getObject(pos));<a name="line.2506"></a>
<span class="sourceLineNo">2507</span>                                        break;<a name="line.2507"></a>
<span class="sourceLineNo">2508</span>                                }<a name="line.2508"></a>
<span class="sourceLineNo">2509</span>                        }<a name="line.2509"></a>
<span class="sourceLineNo">2510</span>                        if (j == n) {<a name="line.2510"></a>
<span class="sourceLineNo">2511</span>                                r.setObjectAbs(i++, d.getObject(pos));<a name="line.2511"></a>
<span class="sourceLineNo">2512</span>                        }<a name="line.2512"></a>
<span class="sourceLineNo">2513</span>                }<a name="line.2513"></a>
<span class="sourceLineNo">2514</span>                return r;<a name="line.2514"></a>
<span class="sourceLineNo">2515</span>        }<a name="line.2515"></a>
<span class="sourceLineNo">2516</span><a name="line.2516"></a>
<span class="sourceLineNo">2517</span>        /**<a name="line.2517"></a>
<span class="sourceLineNo">2518</span>         * Choose content from choices where condition is true, otherwise use default. All inputs are broadcasted to a maximum shape<a name="line.2518"></a>
<span class="sourceLineNo">2519</span>         * @param index integer dataset (ideally, items should be in [0, n) range, if there are n choices)<a name="line.2519"></a>
<span class="sourceLineNo">2520</span>         * @param choices array of datasets or objects<a name="line.2520"></a>
<span class="sourceLineNo">2521</span>         * @param throwAIOOBE if true, throw array index out of bound exception<a name="line.2521"></a>
<span class="sourceLineNo">2522</span>         * @param clip true to clip else wrap indices out of bounds; only used when throwAOOBE is false<a name="line.2522"></a>
<span class="sourceLineNo">2523</span>         * @return dataset<a name="line.2523"></a>
<span class="sourceLineNo">2524</span>         */<a name="line.2524"></a>
<span class="sourceLineNo">2525</span>        public static Dataset choose(IntegerDataset index, Object[] choices, boolean throwAIOOBE, boolean clip) {<a name="line.2525"></a>
<span class="sourceLineNo">2526</span>                final int n = choices.length;<a name="line.2526"></a>
<span class="sourceLineNo">2527</span>                Object[] all = new Object[n + 1];<a name="line.2527"></a>
<span class="sourceLineNo">2528</span>                System.arraycopy(choices, 0, all, 0, n);<a name="line.2528"></a>
<span class="sourceLineNo">2529</span>                all[n] = index;<a name="line.2529"></a>
<span class="sourceLineNo">2530</span>                Dataset[] dChoices = BroadcastUtils.convertAndBroadcast(all);<a name="line.2530"></a>
<span class="sourceLineNo">2531</span>                int dt = -1;<a name="line.2531"></a>
<span class="sourceLineNo">2532</span>                int ds = -1;<a name="line.2532"></a>
<span class="sourceLineNo">2533</span>                int mr = -1;<a name="line.2533"></a>
<span class="sourceLineNo">2534</span>                for (int i = 0; i &lt; n; i++) {<a name="line.2534"></a>
<span class="sourceLineNo">2535</span>                        Dataset a = dChoices[i];<a name="line.2535"></a>
<span class="sourceLineNo">2536</span>                        int r = a.getRank();<a name="line.2536"></a>
<span class="sourceLineNo">2537</span>                        if (r &gt; mr)<a name="line.2537"></a>
<span class="sourceLineNo">2538</span>                                mr = r;<a name="line.2538"></a>
<span class="sourceLineNo">2539</span>                        int t = a.getDType();<a name="line.2539"></a>
<span class="sourceLineNo">2540</span>                        if (t &gt; dt)<a name="line.2540"></a>
<span class="sourceLineNo">2541</span>                                dt = t;<a name="line.2541"></a>
<span class="sourceLineNo">2542</span>                        int s = a.getElementsPerItem();<a name="line.2542"></a>
<span class="sourceLineNo">2543</span>                        if (s &gt; ds)<a name="line.2543"></a>
<span class="sourceLineNo">2544</span>                                ds = s;<a name="line.2544"></a>
<span class="sourceLineNo">2545</span>                }<a name="line.2545"></a>
<span class="sourceLineNo">2546</span>                if (dt &lt; 0 || ds &lt; 1) {<a name="line.2546"></a>
<span class="sourceLineNo">2547</span>                        throw new IllegalArgumentException("Dataset types of choices are invalid");<a name="line.2547"></a>
<span class="sourceLineNo">2548</span>                }<a name="line.2548"></a>
<span class="sourceLineNo">2549</span>                index = (IntegerDataset) dChoices[n];<a name="line.2549"></a>
<span class="sourceLineNo">2550</span>                dChoices[n] = null;<a name="line.2550"></a>
<span class="sourceLineNo">2551</span><a name="line.2551"></a>
<span class="sourceLineNo">2552</span>                @SuppressWarnings("deprecation")<a name="line.2552"></a>
<span class="sourceLineNo">2553</span>                Dataset r = DatasetFactory.zeros(ds, index.getShape(), dt);<a name="line.2553"></a>
<span class="sourceLineNo">2554</span>                IndexIterator iter = index.getIterator(true);<a name="line.2554"></a>
<span class="sourceLineNo">2555</span>                final int[] pos = iter.getPos();<a name="line.2555"></a>
<span class="sourceLineNo">2556</span>                int i = 0;<a name="line.2556"></a>
<span class="sourceLineNo">2557</span>                while (iter.hasNext()) {<a name="line.2557"></a>
<span class="sourceLineNo">2558</span>                        int j = index.getAbs(iter.index);<a name="line.2558"></a>
<span class="sourceLineNo">2559</span>                        if (j &lt; 0) {<a name="line.2559"></a>
<span class="sourceLineNo">2560</span>                                if (throwAIOOBE)<a name="line.2560"></a>
<span class="sourceLineNo">2561</span>                                        throw new ArrayIndexOutOfBoundsException(j);<a name="line.2561"></a>
<span class="sourceLineNo">2562</span>                                if (clip) {<a name="line.2562"></a>
<span class="sourceLineNo">2563</span>                                        j = 0;<a name="line.2563"></a>
<span class="sourceLineNo">2564</span>                                } else {<a name="line.2564"></a>
<span class="sourceLineNo">2565</span>                                        j %= n;<a name="line.2565"></a>
<span class="sourceLineNo">2566</span>                                        j += n; // as remainder still negative<a name="line.2566"></a>
<span class="sourceLineNo">2567</span>                                }<a name="line.2567"></a>
<span class="sourceLineNo">2568</span>                        }<a name="line.2568"></a>
<span class="sourceLineNo">2569</span>                        if (j &gt;= n) {<a name="line.2569"></a>
<span class="sourceLineNo">2570</span>                                if (throwAIOOBE)<a name="line.2570"></a>
<span class="sourceLineNo">2571</span>                                        throw new ArrayIndexOutOfBoundsException(j);<a name="line.2571"></a>
<span class="sourceLineNo">2572</span>                                if (clip) {<a name="line.2572"></a>
<span class="sourceLineNo">2573</span>                                        j = n - 1;<a name="line.2573"></a>
<span class="sourceLineNo">2574</span>                                } else {<a name="line.2574"></a>
<span class="sourceLineNo">2575</span>                                        j %= n;<a name="line.2575"></a>
<span class="sourceLineNo">2576</span>                                }<a name="line.2576"></a>
<span class="sourceLineNo">2577</span>                        }<a name="line.2577"></a>
<span class="sourceLineNo">2578</span>                        Dataset c = dChoices[j];<a name="line.2578"></a>
<span class="sourceLineNo">2579</span>                        r.setObjectAbs(i++, c.getObject(pos));<a name="line.2579"></a>
<span class="sourceLineNo">2580</span>                }<a name="line.2580"></a>
<span class="sourceLineNo">2581</span>                return r;<a name="line.2581"></a>
<span class="sourceLineNo">2582</span>        }<a name="line.2582"></a>
<span class="sourceLineNo">2583</span><a name="line.2583"></a>
<span class="sourceLineNo">2584</span>        /**<a name="line.2584"></a>
<span class="sourceLineNo">2585</span>         * Calculate positions in given shape from a dataset of 1-D indexes<a name="line.2585"></a>
<span class="sourceLineNo">2586</span>         * @param indices<a name="line.2586"></a>
<span class="sourceLineNo">2587</span>         * @param shape<a name="line.2587"></a>
<span class="sourceLineNo">2588</span>         * @return list of positions as integer datasets<a name="line.2588"></a>
<span class="sourceLineNo">2589</span>         */<a name="line.2589"></a>
<span class="sourceLineNo">2590</span>        public static List&lt;IntegerDataset&gt; calcPositionsFromIndexes(Dataset indices, int[] shape) {<a name="line.2590"></a>
<span class="sourceLineNo">2591</span>                int rank = shape.length;<a name="line.2591"></a>
<span class="sourceLineNo">2592</span>                List&lt;IntegerDataset&gt; posns = new ArrayList&lt;IntegerDataset&gt;();<a name="line.2592"></a>
<span class="sourceLineNo">2593</span>                int[] iShape = indices.getShapeRef();<a name="line.2593"></a>
<span class="sourceLineNo">2594</span>                for (int i = 0; i &lt; rank; i++) {<a name="line.2594"></a>
<span class="sourceLineNo">2595</span>                        posns.add(new IntegerDataset(iShape));<a name="line.2595"></a>
<span class="sourceLineNo">2596</span>                }<a name="line.2596"></a>
<span class="sourceLineNo">2597</span>                IndexIterator it = indices.getIterator(true);<a name="line.2597"></a>
<span class="sourceLineNo">2598</span>                int[] pos = it.getPos();<a name="line.2598"></a>
<span class="sourceLineNo">2599</span>                while (it.hasNext()) {<a name="line.2599"></a>
<span class="sourceLineNo">2600</span>                        int n = indices.getInt(pos);<a name="line.2600"></a>
<span class="sourceLineNo">2601</span>                        int[] p = ShapeUtils.getNDPositionFromShape(n, shape);<a name="line.2601"></a>
<span class="sourceLineNo">2602</span>                        for (int i = 0; i &lt; rank; i++) {<a name="line.2602"></a>
<span class="sourceLineNo">2603</span>                                posns.get(i).setItem(p[i], pos);<a name="line.2603"></a>
<span class="sourceLineNo">2604</span>                        }<a name="line.2604"></a>
<span class="sourceLineNo">2605</span>                }<a name="line.2605"></a>
<span class="sourceLineNo">2606</span>                return posns;<a name="line.2606"></a>
<span class="sourceLineNo">2607</span>        }<a name="line.2607"></a>
<span class="sourceLineNo">2608</span><a name="line.2608"></a>
<span class="sourceLineNo">2609</span><a name="line.2609"></a>
<span class="sourceLineNo">2610</span>        /**<a name="line.2610"></a>
<span class="sourceLineNo">2611</span>         * Calculate indexes in given shape from datasets of position<a name="line.2611"></a>
<span class="sourceLineNo">2612</span>         * @param positions as a list of datasets where each holds the position in a dimension<a name="line.2612"></a>
<span class="sourceLineNo">2613</span>         * @param shape<a name="line.2613"></a>
<span class="sourceLineNo">2614</span>         * @param mode either null, zero-length, unit length or length of rank of shape where<a name="line.2614"></a>
<span class="sourceLineNo">2615</span>         *  0 = raise exception, 1 = wrap, 2 = clip<a name="line.2615"></a>
<span class="sourceLineNo">2616</span>         * @return indexes as an integer dataset<a name="line.2616"></a>
<span class="sourceLineNo">2617</span>         */<a name="line.2617"></a>
<span class="sourceLineNo">2618</span>        public static IntegerDataset calcIndexesFromPositions(List&lt;? extends Dataset&gt; positions, int[] shape, int... mode) {<a name="line.2618"></a>
<span class="sourceLineNo">2619</span>                int rank = shape.length;<a name="line.2619"></a>
<span class="sourceLineNo">2620</span>                if (positions.size() != rank) {<a name="line.2620"></a>
<span class="sourceLineNo">2621</span>                        throw new IllegalArgumentException("Number of position datasets must be equal to rank of shape");<a name="line.2621"></a>
<span class="sourceLineNo">2622</span>                }<a name="line.2622"></a>
<span class="sourceLineNo">2623</span><a name="line.2623"></a>
<span class="sourceLineNo">2624</span>                if (mode == null || mode.length == 0) {<a name="line.2624"></a>
<span class="sourceLineNo">2625</span>                        mode = new int[rank];<a name="line.2625"></a>
<span class="sourceLineNo">2626</span>                } else if (mode.length == 1) {<a name="line.2626"></a>
<span class="sourceLineNo">2627</span>                        int m = mode[0];<a name="line.2627"></a>
<span class="sourceLineNo">2628</span>                        mode = new int[rank];<a name="line.2628"></a>
<span class="sourceLineNo">2629</span>                        Arrays.fill(mode, m);<a name="line.2629"></a>
<span class="sourceLineNo">2630</span>                } else if (mode.length != rank) {<a name="line.2630"></a>
<span class="sourceLineNo">2631</span>                        throw new IllegalArgumentException("Mode length greater than one must match rank of shape");<a name="line.2631"></a>
<span class="sourceLineNo">2632</span>                }<a name="line.2632"></a>
<span class="sourceLineNo">2633</span>                for (int i = 0; i &lt; rank; i++) {<a name="line.2633"></a>
<span class="sourceLineNo">2634</span>                        int m = mode[i];<a name="line.2634"></a>
<span class="sourceLineNo">2635</span>                        if (m &lt; 0 || m &gt; 2) {<a name="line.2635"></a>
<span class="sourceLineNo">2636</span>                                throw new IllegalArgumentException("Unknown mode value - it must be 0, 1, or 2");<a name="line.2636"></a>
<span class="sourceLineNo">2637</span>                        }<a name="line.2637"></a>
<span class="sourceLineNo">2638</span>                }<a name="line.2638"></a>
<span class="sourceLineNo">2639</span><a name="line.2639"></a>
<span class="sourceLineNo">2640</span>                Dataset p = positions.get(0);<a name="line.2640"></a>
<span class="sourceLineNo">2641</span>                IntegerDataset indexes = new IntegerDataset(p.getShapeRef());<a name="line.2641"></a>
<span class="sourceLineNo">2642</span>                IndexIterator it = p.getIterator(true);<a name="line.2642"></a>
<span class="sourceLineNo">2643</span>                int[] iPos = it.getPos();<a name="line.2643"></a>
<span class="sourceLineNo">2644</span>                int[] tPos = new int[rank];<a name="line.2644"></a>
<span class="sourceLineNo">2645</span>                while (it.hasNext()) {<a name="line.2645"></a>
<span class="sourceLineNo">2646</span>                        for (int i = 0; i &lt; rank; i++) {<a name="line.2646"></a>
<span class="sourceLineNo">2647</span>                                p = positions.get(i);<a name="line.2647"></a>
<span class="sourceLineNo">2648</span>                                int j = p.getInt(iPos);<a name="line.2648"></a>
<span class="sourceLineNo">2649</span>                                int d = shape[i];<a name="line.2649"></a>
<span class="sourceLineNo">2650</span>                                if (mode[i] == 0) {<a name="line.2650"></a>
<span class="sourceLineNo">2651</span>                                        if (j &lt; 0 || j &gt;= d) {<a name="line.2651"></a>
<span class="sourceLineNo">2652</span>                                                throw new ArrayIndexOutOfBoundsException("Position value exceeds dimension in shape");<a name="line.2652"></a>
<span class="sourceLineNo">2653</span>                                        }<a name="line.2653"></a>
<span class="sourceLineNo">2654</span>                                } else if (mode[i] == 1) {<a name="line.2654"></a>
<span class="sourceLineNo">2655</span>                                        while (j &lt; 0)<a name="line.2655"></a>
<span class="sourceLineNo">2656</span>                                                j += d;<a name="line.2656"></a>
<span class="sourceLineNo">2657</span>                                        while (j &gt;= d)<a name="line.2657"></a>
<span class="sourceLineNo">2658</span>                                                j -= d;<a name="line.2658"></a>
<span class="sourceLineNo">2659</span>                                } else {<a name="line.2659"></a>
<span class="sourceLineNo">2660</span>                                        if (j &lt; 0)<a name="line.2660"></a>
<span class="sourceLineNo">2661</span>                                                j = 0;<a name="line.2661"></a>
<span class="sourceLineNo">2662</span>                                        if (j &gt;= d)<a name="line.2662"></a>
<span class="sourceLineNo">2663</span>                                                j = d - 1;<a name="line.2663"></a>
<span class="sourceLineNo">2664</span>                                }<a name="line.2664"></a>
<span class="sourceLineNo">2665</span>                                tPos[i] = j;<a name="line.2665"></a>
<span class="sourceLineNo">2666</span>                        }<a name="line.2666"></a>
<span class="sourceLineNo">2667</span>                        indexes.set(ShapeUtils.getFlat1DIndex(shape, tPos), iPos);<a name="line.2667"></a>
<span class="sourceLineNo">2668</span>                }<a name="line.2668"></a>
<span class="sourceLineNo">2669</span><a name="line.2669"></a>
<span class="sourceLineNo">2670</span>                return indexes;<a name="line.2670"></a>
<span class="sourceLineNo">2671</span>        }<a name="line.2671"></a>
<span class="sourceLineNo">2672</span><a name="line.2672"></a>
<span class="sourceLineNo">2673</span>        /**<a name="line.2673"></a>
<span class="sourceLineNo">2674</span>         * Serialize dataset by flattening it. Discards metadata<a name="line.2674"></a>
<span class="sourceLineNo">2675</span>         * @param data<a name="line.2675"></a>
<span class="sourceLineNo">2676</span>         * @return some java array<a name="line.2676"></a>
<span class="sourceLineNo">2677</span>         */<a name="line.2677"></a>
<span class="sourceLineNo">2678</span>        public static Serializable serializeDataset(final IDataset data) {<a name="line.2678"></a>
<span class="sourceLineNo">2679</span>                Dataset d = convertToDataset(data.getSliceView());<a name="line.2679"></a>
<span class="sourceLineNo">2680</span>                d.clearMetadata(null);<a name="line.2680"></a>
<span class="sourceLineNo">2681</span>                return d.flatten().getBuffer();<a name="line.2681"></a>
<span class="sourceLineNo">2682</span>        }<a name="line.2682"></a>
<span class="sourceLineNo">2683</span><a name="line.2683"></a>
<span class="sourceLineNo">2684</span>        /**<a name="line.2684"></a>
<span class="sourceLineNo">2685</span>         * Extract values where condition is non-zero. This is similar to Dataset#getByBoolean but supports broadcasting<a name="line.2685"></a>
<span class="sourceLineNo">2686</span>         * @param data<a name="line.2686"></a>
<span class="sourceLineNo">2687</span>         * @param condition should be broadcastable to data<a name="line.2687"></a>
<span class="sourceLineNo">2688</span>         * @return 1-D dataset of values<a name="line.2688"></a>
<span class="sourceLineNo">2689</span>         */<a name="line.2689"></a>
<span class="sourceLineNo">2690</span>        @SuppressWarnings("deprecation")<a name="line.2690"></a>
<span class="sourceLineNo">2691</span>        public static Dataset extract(final IDataset data, final IDataset condition) {<a name="line.2691"></a>
<span class="sourceLineNo">2692</span>                Dataset a = convertToDataset(data.getSliceView());<a name="line.2692"></a>
<span class="sourceLineNo">2693</span>                Dataset b = cast(condition.getSliceView(), Dataset.BOOL);<a name="line.2693"></a>
<span class="sourceLineNo">2694</span><a name="line.2694"></a>
<span class="sourceLineNo">2695</span>                try {<a name="line.2695"></a>
<span class="sourceLineNo">2696</span>                        return a.getByBoolean(b);<a name="line.2696"></a>
<span class="sourceLineNo">2697</span>                } catch (IllegalArgumentException e) {<a name="line.2697"></a>
<span class="sourceLineNo">2698</span>                        final int length = ((Number) b.sum()).intValue();<a name="line.2698"></a>
<span class="sourceLineNo">2699</span><a name="line.2699"></a>
<span class="sourceLineNo">2700</span>                        BroadcastPairIterator it = new BroadcastPairIterator(a, b, null, false);<a name="line.2700"></a>
<span class="sourceLineNo">2701</span>                        int size = ShapeUtils.calcSize(it.getShape());<a name="line.2701"></a>
<span class="sourceLineNo">2702</span>                        Dataset c;<a name="line.2702"></a>
<span class="sourceLineNo">2703</span>                        if (length &lt; size) {<a name="line.2703"></a>
<span class="sourceLineNo">2704</span>                                int[] ashape = it.getFirstShape();<a name="line.2704"></a>
<span class="sourceLineNo">2705</span>                                int[] bshape = it.getSecondShape();<a name="line.2705"></a>
<span class="sourceLineNo">2706</span>                                int r = ashape.length;<a name="line.2706"></a>
<span class="sourceLineNo">2707</span>                                size = length;<a name="line.2707"></a>
<span class="sourceLineNo">2708</span>                                for (int i = 0; i &lt; r; i++) {<a name="line.2708"></a>
<span class="sourceLineNo">2709</span>                                        int s = ashape[i];<a name="line.2709"></a>
<span class="sourceLineNo">2710</span>                                        if (s &gt; 1 &amp;&amp; bshape[i] == 1) {<a name="line.2710"></a>
<span class="sourceLineNo">2711</span>                                                size *= s;<a name="line.2711"></a>
<span class="sourceLineNo">2712</span>                                        }<a name="line.2712"></a>
<span class="sourceLineNo">2713</span>                                }<a name="line.2713"></a>
<span class="sourceLineNo">2714</span>                        }<a name="line.2714"></a>
<span class="sourceLineNo">2715</span>                        c = DatasetFactory.zeros(new int[] {size}, a.getDType());<a name="line.2715"></a>
<span class="sourceLineNo">2716</span><a name="line.2716"></a>
<span class="sourceLineNo">2717</span>                        int i = 0;<a name="line.2717"></a>
<span class="sourceLineNo">2718</span>                        if (it.isOutputDouble()) {<a name="line.2718"></a>
<span class="sourceLineNo">2719</span>                                while (it.hasNext()) {<a name="line.2719"></a>
<span class="sourceLineNo">2720</span>                                        if (it.bLong != 0) {<a name="line.2720"></a>
<span class="sourceLineNo">2721</span>                                                c.setObjectAbs(i++, it.aDouble);<a name="line.2721"></a>
<span class="sourceLineNo">2722</span>                                        }<a name="line.2722"></a>
<span class="sourceLineNo">2723</span>                                }<a name="line.2723"></a>
<span class="sourceLineNo">2724</span>                        } else {<a name="line.2724"></a>
<span class="sourceLineNo">2725</span>                                while (it.hasNext()) {<a name="line.2725"></a>
<span class="sourceLineNo">2726</span>                                        if (it.bLong != 0) {<a name="line.2726"></a>
<span class="sourceLineNo">2727</span>                                                c.setObjectAbs(i++, it.aLong);<a name="line.2727"></a>
<span class="sourceLineNo">2728</span>                                        }<a name="line.2728"></a>
<span class="sourceLineNo">2729</span>                                }<a name="line.2729"></a>
<span class="sourceLineNo">2730</span>                        }<a name="line.2730"></a>
<span class="sourceLineNo">2731</span><a name="line.2731"></a>
<span class="sourceLineNo">2732</span>                        return c;<a name="line.2732"></a>
<span class="sourceLineNo">2733</span>                }<a name="line.2733"></a>
<span class="sourceLineNo">2734</span>        }<a name="line.2734"></a>
<span class="sourceLineNo">2735</span>}<a name="line.2735"></a>




























































</pre>
</div>
</body>
</html>
